简体   繁体   English

比较2个对象数组,如果它存在于第二个数组中,则返回第一个数组中的元素为true,否则返回false

[英]compare 2 object arrays and return the elements in first array as true if it exists in second array, or as false if it does not

I have 2 object arrays 我有2个对象数组

const options = [
  { value: 'opt1', label: 'opt1' },
  { value: 'opt2', label: 'opt2' },
  { value: 'opt3', label: 'opt3' },
  { value: 'opt4', label: 'opt4' }
]

const selected = [
  { value: 'opt1', key: '1' },
  { value: 'opt2', key: '2' }
]

I need to compare these two arrays and get result as 我需要比较这两个数组并得到结果

result =
  { 'opt1', true },
  { 'opt2', true },
  { 'opt3', false },
  { 'opt4', false }
]

since opt1 and opt2 exists in second array. 因为opt1和opt2存在于第二个数组中。 I know there are lots of methods, but what would be the shortest method? 我知道有很多方法,但最短的方法是什么?

我个人可以想象的最短的一个。

const result = options.map(o => ({ [o.value]: !!selected.find(s => s.value === o.value) }));

You can use the function map to get the mapped values first of the selected and then a map the array options using a logic to ask for the existence of a value within the selected array. 您可以使用函数map首先获取所选映射值,然后使用逻辑映射数组options以询问selected数组中是否存在值。 Finally, use computed-property-names to build the desired output. 最后,使用computed-property-names来构建所需的输出。

 const options = [ { value: 'opt1', label: 'opt1' }, { value: 'opt2', label: 'opt2' }, { value: 'opt3', label: 'opt3' }, { value: 'opt4', label: 'opt4' }], selected = [ { value: 'opt1', key: '1' }, { value: 'opt2',key: '2' }], mapped = selected.map(({value}) => value), result = options.map(({value}) => ({[value]: mapped.includes(value)})); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

return options.map(v => {
  let newObj = {};
  newObj[v.value] = selected.find(option => { return 
  option.value == v.value }) !== undefined;
  return newObj;
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 比较 2 个数组并返回 true 或 false - Compare 2 arrays and return true or false 对象的 arrays 数组 - 查找 object 值出现并返回真/假 (js) - Array of arrays of objects - find object value occurance and return true/false (js) 比较两个 Arrays 对象,如果 Javascript 中的值为真,则返回一个新数组 - Compare Two Arrays Of Objects and return a new array if value is true in Javascript TS 2 arrays,如果第一个数组中的项目存在于第二个数组中,则 append 到第二个数组的开头 - TS 2 arrays, if item from first array exists in second array then append to start of second array 将true / false数组与其他数组进行比较 - Compare true/false array with other array Javascript检查数组中的元素是否为相同类型并返回true / false - Javascript check if elements in array are same type and return true/false Javascript比较两个不同大小的数组,并返回不在第二个数组中的项目 - Javascript Compare two arrays with different sizes and return the items that are not in second array 检查 object 数组中的所有值是否等于 false 并在 react 中返回 true - Check if all the values in array of object is equal to false and return true in react 比较多个数组的数组元素 - compare elements of array of many arrays 如何比较 2 个数组的相同值并将它们保存到带有 true 或 false 标志的第三个数组 - How to compare 2 arrays for same value and save them to 3rd array with true or false flags
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM