2

我想找到两个数组之间的匹配值,并创建一个 json 数组,如果值匹配,则设置为 true,如果不匹配,则设置为 false。我知道,secondArray 中的值将始终与第一个数组中的某些值匹配,并且它总是更小,因为 secondArray 是基于第一个数组创建的。

let firstArray = ["One", "Two", "Three", "Four", "Five"];
let secondArray = ["Three", "Four"];
let jsonArray = [];

我想创建一个 json 数组:

[
  {
    "name": "One",
    "matched": false
  },
  {
    "name": "Two",
    "matched": false
  },
  {
    "name": "Three",
    "matched": true
  },
  {
    "name": "Four",
    "matched": true
  },
  {
    "name": "Five",
    "matched": false
  }
]

通常,我会做这样的事情:

            firstArray.forEach(firstElement=>{
              secondArray.forEach(secondElement=>{
                  if(firstArray.indexOf(secondElement)>=0){
                      jsonArray.push({'name': secondElement, 'matched': true});
                  }else{
                      jsonArray.push({'name': secondElement, 'matched': false});
                  }
              });
          });

但这只是创建了一个具有重复值的 json 数组,其中名称相同,但匹配的值是 false 和 true。

似乎我迷失在一些非常简单的事情中。

4

6 回答 6

3

您可以使用mapincludeshelper 的组合来实现这一点。

let firstArray = ["One", "Two", "Three", "Four", "Five"];
let secondArray = ["Three", "Four"];
let jsonArray = [];

jsonArray = firstArray.map(i => {
   return { 'name': i, 'matched': secondArray.includes(i) };
});

console.log(jsonArray);

于 2018-06-20T15:36:40.477 回答
1

这里的所有其他解决方案都执行了不必要的计算;它们的运行时间随着数组长度的平方而增长。尝试使用大小为 100k+ 的数组运行它们 :-)

您正在寻找的解决方案非常简单,并且运行在O(n)

let firstArray = ["One", "Two", "Three", "Four", "Five"];
let secondArray = ["Three", "Four"];

let map = {};
firstArray.forEach(i => map[i] = false);
secondArray.forEach(i => map[i] === false && (map[i] = true));
let jsonArray = Object.keys(map).map(k => ({ name: k, matched: map[k] }));
于 2018-06-20T15:24:05.123 回答
0

我们可以用

Array.prototype.includes()

检查数组中是否存在元素

let firstArray = ["One", "Two", "Three", "Four", "Five"];
let secondArray = ["Three", "Four"];
let jsonArray = [];

firstArray.forEach(val=>{

if(secondArray.includes(val))
{
  jsonArray.push({'name': val, 'matched': true})

}else{
  jsonArray.push({'name': val, 'matched': false})
}

})

console.log(jsonArray);

于 2018-06-20T14:49:32.910 回答
0

你可以通过两种方式做到这一点。

Way1:
let array1 = ['a','b','c','d'];
let array2 = ['b','d'];
let commonArray = [];

array1.forEach( x => {
    if(array2.indexOf(x) != -1){
      commonArray.push(x);
    }
});`

console.log(commonArray);   //commonArray is the resulting array

Way2: Use intersection of Underscore.js

At first you have to import underscore:  
import * as _ from 'underscore';`  

Then use intersection to calculate common.  
commonArray = _.intersection(array1, array2);

console.log(commonArray);   //commonArray is the resulting array
于 2018-06-21T13:51:39.343 回答
0

使用 thfind检查元素是否存在

firstArray.forEach(secondElement=>{
  let exist = secondArray.find((item) => item === secondElement);
  if(exist){
    jsonArray.push({'name': secondElement, 'matched': true})
  }else{

    jsonArray.push({'name': secondElement, 'matched': false})
  }
});

演示

于 2018-06-20T14:40:07.890 回答
0

尝试这个:

  let firstArray = ["One", "Two", "Three". "Four", "Five"];
  let secondArray = ["Three", "Four"];
  let jsonArray = [];
  firstArray.forEach(firstElement=>{
        if(secondArray.indexOf(firstElement)>=0){
            jsonArray.push({'name': firstElement, 'matched': true});
        }else{
            jsonArray.push({'name': firstElement, 'matched': false});
        }
 });

希望这段代码对你有帮助

于 2018-06-20T14:40:44.300 回答