查找 JS 对象中的重复或虚假值 - Javascript

我有一个对象,它可以包含重复和/或虚假值。我想基于该对象组成一个对象数组,并根据对不区分大小写的值的检查添加一个新的布尔属性。


这就是我所拥有的:


const obj = {

  a: 'A',

  b: 'B',

  c: 'C',

  d: 'c',

  e: 'E',

  f: ''

}


console.log(Object.keys(obj).map(i => {

  return {

    key: i,

    isDuplicateOrFalsy: _.filter(

        Object.values(obj),

        j =>

        _.trimEnd(_.toLower(j)) ===

        _.trimEnd(

          _.toLower(

            obj[i]

          )

        )

      ).length > 1 ||

      !_.every(

        Object.values(obj),

        Boolean

      )

  }

}))

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"></script>

预期输出:


[{

  isDuplicateOrFalsy: false,

  key: "a"

}, {

  isDuplicateOrFalsy: false,

  key: "b"

}, {

  isDuplicateOrFalsy: true,

  key: "c"

}, {

  isDuplicateOrFalsy: true,

  key: "d"

}, {

  isDuplicateOrFalsy: false,

  key: "e"

}, {

  isDuplicateOrFalsy: true,

  key: "f"

}]

请指教。


收到一只叮咚
浏览 144回答 5
5回答

手掌心

将对象转换为[key, value]with的条目_.toPairs(),并按值的小写版本对它们进行分组。平面映射组,并将组中的每个条目映射回一个对象。组内长度大于 1 的任何项目都是重复项。合并对象,并使用以下命令以正确的顺序获取项目_.at():const fn = obj => _.at(&nbsp; _.merge(..._.flatMap(&nbsp; &nbsp; _.groupBy(_.toPairs(obj), ([, v]) => _.lowerCase(v)),&nbsp; &nbsp; group => group.map(([key, v]) => ( { [key]:{&nbsp; &nbsp; &nbsp; key,&nbsp; &nbsp; &nbsp; isDuplicateOrFalsy: group.length > 1 || _.isEmpty(_.trim(v))&nbsp; &nbsp; }}))&nbsp; )),&nbsp; _.keys(obj)&nbsp;)const obj = {"a":"A","b":"B","c":"C","d":"C","e":"E","f":"","g":"c"}const result = fn(obj)console.log(result)<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"></script>

隔江千里

你可以做类似的事情:const obj = { a: 'A', b: 'B', c: 'C', d: 'C', e: 'E', f: '' };const res = Object.entries(obj)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(([key, val], i, arr) => ({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; key,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isDuplicateOrFalsy: !val ||&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr.some(([k, v], j) =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j !== i && v.toLowerCase().trim() === val.toLowerCase().trim()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;console.log(res);

哔哔one

解决方案不包含不必要的循环:const obj = {&nbsp; a: 'A',&nbsp; b: 'B',&nbsp; c: 'C',&nbsp; d: 'C',&nbsp; e: 'E',&nbsp; f: ''}// make array [ [key, value], ...&nbsp; ] and sort by values&nbsp;values = Object.entries(obj).sort((a,b)&nbsp; => a[1] > b[1])result = values.map((e,i, arr) => {&nbsp; const [key, value] = e;&nbsp; const last = values.length -1; // last index&nbsp; let isDuplicateOrFalsy = false;&nbsp;&nbsp;&nbsp; // true conditions = dublicates are near&nbsp; if (!value) isDuplicateOrFalsy = true; // falsy check&nbsp; else if (i > 0 && i < last&nbsp; // for middle&nbsp; &nbsp; &nbsp; && (arr[i-1][1] === value || arr[i+1][1] === value))&nbsp; isDuplicateOrFalsy = true;&nbsp; &nbsp;&nbsp; else if (i === 0 && arr[1][1] === value) isDuplicateOrFalsy = true; // for first&nbsp; else if (i === last && arr[last-1][1] === value) isDuplicateOrFalsy = true;&nbsp; &nbsp;// for last&nbsp; return {&nbsp; &nbsp; key,&nbsp; &nbsp; isDuplicateOrFalsy&nbsp; }})console.log(result)

森栏

const obj = {&nbsp; a: 'A',&nbsp; b: 'B',&nbsp; c: 'C',&nbsp; d: 'c',&nbsp; e: 'E',&nbsp; f: ''};const isDuplicateOrFalsyByValue = Object&nbsp; .values(obj)&nbsp; .reduce(&nbsp; &nbsp; (result, value) => {&nbsp; &nbsp; &nbsp; const caseInsensetiveValue = value.toLowerCase();&nbsp; &nbsp; &nbsp; result[caseInsensetiveValue] = result[caseInsensetiveValue] === undefined&nbsp; &nbsp; &nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * If `caseInsensetiveValue` is a falsy value,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; then set `isDuplicateOrFalsy` to `true`&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * Otherwise set it to `false`&nbsp; &nbsp; &nbsp; &nbsp; */&nbsp; &nbsp; &nbsp; &nbsp; ? !caseInsensetiveValue&nbsp; &nbsp; &nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * If result[caseInsensetiveValue] is `true` (we had a falsy value),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; then this `true` won't hurt&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * Otherwise we have a duplicate at this point&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; and should set it to `true` as well.&nbsp; &nbsp; &nbsp; &nbsp; */&nbsp; &nbsp; &nbsp; &nbsp; : true;&nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; },&nbsp; &nbsp; {},&nbsp; );const keysWithDuplicationOrFalsyInfo = Object&nbsp; .entries(obj)&nbsp; .reduce(&nbsp; &nbsp; (result, [key, value]) => [&nbsp; &nbsp; &nbsp; ...result,&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; isDuplicateOrFalsy: isDuplicateOrFalsyByValue[value.toLowerCase()],&nbsp; &nbsp; &nbsp; &nbsp; key,&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; ],&nbsp; &nbsp; [],&nbsp; );console.log('keysWithDuplicationOrFalsyInfo');console.log(keysWithDuplicationOrFalsyInfo);

潇潇雨雨

简短且更易读。const obj = {&nbsp; a: 'A',&nbsp; b: 'B',&nbsp; c: 'C',&nbsp; d: 'c',&nbsp; e: 'E',&nbsp; f: ''}// Object map number of occurance of each value. { a: 1, b: 1, c: 2, d: 1 }const valuesOccurance = _.mapValues(_.groupBy(obj, _.lowerCase), occurances => occurances.length);// function to check duplicateconst isDuplicate = value => valuesOccurance[_.lowerCase(value)] > 1;// function to check falsy valueconst isFalsy = value => !value;const result = _.map(obj, (value, key) => {&nbsp; return {&nbsp; &nbsp; isDuplicateOrFalsy: isFalsy(value) || isDuplicate(value),&nbsp; &nbsp; key,&nbsp; };});console.log({ result })<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"></script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript