【问题标题】:2 arrays matchings , transformation in arrays of objects [closed]2个数组匹配,对象数组中的转换[关闭]
【发布时间】:2021-05-21 08:46:35
【问题描述】:

是否可以比较 2 个数组并为每个项目使用字符串值和布尔值创建对象:

const array = ["ABC", "BCA", "CDA", "APA"];
const array2 = ["ABC", "APA"];

array2 = [{
    value: "ABC",
    matched: true,
  },
  {
    value: "BCA",
    matched: false,
  },
  {
    value: "CDA",
    matched: false,
  },
  {
    value: "APA",
    matched: true,
  },
];

【问题讨论】:

  • 如果array2 中还有一个不匹配的项目怎么办?你试过什么?

标签: javascript arrays angular


【解决方案1】:

是的:

let array = ["ABC", "BCA", "CDA", "APA"]
let array2 = ["ABC", "APA"]

const output = array.map((item) => {
  let isFound = array2.includes(item);
  return { value: item, matched: isFound };
})

console.log(output)
猜你喜欢
  • 2022-12-31
  • 2020-03-22
  • 2020-10-05
  • 2019-09-15
  • 2020-07-14
  • 1970-01-01
  • 2019-07-29
  • 2011-09-04
相关资源
最近更新 更多