我正在尝试将标志设置为true,以匹配其字段值与cars1变量匹配的myArray记录。
现在,cars1和cars2记录彼此之间的关系为1对1,这意味着汽车的t1分别指向汽车2的abc。
当匹配myarray与Cars1匹配时,如果找到匹配,那么我想记下cars1的索引,并根据该索引我想从cars2获取记录并在该索引位置分配给array2。
例如:myArray的t1和cars1的t1匹配,因此t1的索引为0因此基于此索引从cars2获取第0个索引记录并在第0个索引处分配给array2。因为t3 pqr将在array2中的第2个位置
array2中的预期输出:
array2 = ["abc" ,undefined, "pqr",undefined,"lmn" ];
但是在这里我没有得到如何记录Cars1的匹配记录的索引并在该索引位置分配给array2。
var myArray = [
{field: 't1', flag: false},
{field: 't2', flag: false},
{field: 't3', flag: false},
{field: 't4', flag: false},
{field: 't5', flag: false}
];
var cars1 = ["t1", "t3","t5"];
var cars2 = ["abc" ,"pqr", "lmn" ];
var array2 =[];
myArray.forEach(car => car.flag = cars1.indexOf(car.field) > -1);
console.log(myArray)
答案 0 :(得分:4)
重复搜索方式(适用于小型数据集)和构建地图方式(适用于大型数据集)。
您可以map
上的myArray
执行此操作,在car.field
中查找cars1
:然后如果发现从cars2
返回该条目:
const array2 = myArray.map(car => {
const index = cars1.indexOf(car.field);
if (index === -1) {
return undefined;
}
car.flag = true;
return cars2[index];
});
ES5及更早版本:
var array2 = myArray.map(function(car) {
var index = cars1.indexOf(car.field);
if (index === -1) {
return undefined;
}
car.flag = true;
return cars2[index];
});
请注意,这会使用带有副作用的Array.prototype.map
(设置flag
)。纯粹主义者会说map
不应该有副作用。
示例:
const myArray = [
{field: 't1', flag: false},
{field: 't2', flag: false},
{field: 't3', flag: false},
{field: 't4', flag: false},
{field: 't5', flag: false}
];
const cars1 = ["t1", "t3","t5"];
const cars2 = ["abc", "pqr", "lmn"];
const array2 = myArray.map(car => {
const index = cars1.indexOf(car.field);
if (index === -1) {
return undefined;
}
car.flag = true;
return cars2[index];
});
console.log(array2);
console.log(myArray);

.as-console-wrapper {
max-height: 100% !important;
}

请注意,它会在cars1
上重复执行线性搜索,这对于示例数据和大多数用例来说都是绝对正常的。如果cars1
在数十万个条目中,您可能需要构建一个将值映射到索引的映射,以便您更有效地查找。
构建地图(在一个对象中,因为键是字符串而且它们具有疯狂效率):
const cars1Map = Object.create(null);
cars1.forEach((entry, index) => {
cars1Map[entry] = index;
});
ES5及更早版本(但对于< ES5,您需要Object.create
的polyfill):
var cars1Map = Object.create(null);
cars1.forEach(function(entry, index) {
cars1Map[entry] = index;
});
使用它:
const array2 = myArray.map(car => {
const index = cars1Map[car.field];
if (index === undefined) {
return undefined;
}
car.flag = true;
return cars2[index];
});
ES5及更早版本:
var array2 = myArray.map(function(car) {
var index = cars1Map[car.field];
if (index === undefined) {
return undefined;
}
car.flag = true;
return cars2[index];
});
const myArray = [
{field: 't1', flag: false},
{field: 't2', flag: false},
{field: 't3', flag: false},
{field: 't4', flag: false},
{field: 't5', flag: false}
];
const cars1 = ["t1", "t3","t5"];
const cars2 = ["abc", "pqr", "lmn"];
const cars1Map = Object.create(null);
cars1.forEach((entry, index) => {
cars1Map[entry] = index;
});
const array2 = myArray.map(car => {
const index = cars1Map[car.field];
if (index === undefined) {
return undefined;
}
car.flag = true;
return cars2[index];
});
console.log(array2);
console.log(myArray);

.as-console-wrapper {
max-height: 100% !important;
}

或者纯粹主义者可能会说在ES2015 +中,您应该使用Map
(如果您的密钥不是字符串,那么您肯定想要Map
):
建立它:
const cars1Map = new Map();
cars1.forEach((entry, index) => {
cars1Map.set(entry, index);
});
使用它:
const array2 = myArray.map(car => {
const index = cars1Map.get(car.field)
if (index === undefined) {
return undefined;
}
car.flag = true;
return cars2[index];
});
(如果你不想依赖在地图中查找undefined
,你可以添加一名警卫。)
const myArray = [
{field: 't1', flag: false},
{field: 't2', flag: false},
{field: 't3', flag: false},
{field: 't4', flag: false},
{field: 't5', flag: false}
];
const cars1 = ["t1", "t3","t5"];
const cars2 = ["abc", "pqr", "lmn"];
const cars1Map = new Map();
cars1.forEach((entry, index) => {
cars1Map.set(entry, index);
});
const array2 = myArray.map(car => {
const index = cars1Map.get(car.field)
if (index === undefined) {
return undefined;
}
car.flag = true;
return cars2[index];
});
console.log(array2);
console.log(myArray);

.as-console-wrapper {
max-height: 100% !important;
}

* (这是我贫穷的小博客上的帖子)