将一个对象的值匹配到另一个对象
Match values from one object to another
我有两个对象数组:
[
0: {key1: value1, key2: value2, key3: value3},
1: {key1: value1, key2: value2, key3: value3}
]
[
0: {stop_id: 173, file_id: "1", key_type: null, key_value: "0020", seg_beg: 32},
1: {stop_id: 176, file_id: "1", key_type: null, key_value: "0201", seg_beg: 10},
2: {stop_id: 176, file_id: "1", key_type: null, key_value: "0201", seg_beg: 10}
]
我需要检查第一个对象中任何键的值是否与 key_value...键的任何值匹配, 在第二个对象中,然后将变量进一步设置为匹配记录中的 stop_id 值。像这样:
if(object1.value === object2.key_value){
match = object2[iterator].stop_id;
}
为了简化这一点,我试图只获取第一个对象的值:
//pd.segs is object 1
let pdSegValues = [];
for(let i=0;i<pd.segs.length;i++){
pdSegValues.push(Object.values(pd.segs[i]));
}
但这又让我得到了一个数组数组,基本上让我回到了同样的情况。我的脑子坏了,不可否认,我对循环有一个弱点。任何人都可以告诉我一个体面的方法来完成我在这里需要的东西吗?
您可以通过收集要测试的值然后使用 some
.
来完成此操作
let arr1 = [
{"a1": "value1", "b1": "value2"},
{"a2": "0020", "b2": "value22"},
{"a3": "value111", "b3": "0201"}
];
let arr2 = [
{stop_id: 173, file_id: "1", key_type: null, key_value: "0020", seg_beg: 32},
{stop_id: 176, file_id: "1", key_type: null, key_value: "0201", seg_beg: 10},
{stop_id: 176, file_id: "1", key_type: null, key_value: "0201", seg_beg: 10}
];
// accumulate unique arr1 values to an array
let arr1Values = Array.from(arr1.reduce((acc, curr) => {
Object.values(curr).forEach(v => acc.add(v));
return acc;
}, new Set()));
// accumulate all unique arr2 "key_value"
let arr2KeyValues = arr2.reduce((acc, curr) => {
acc.add(curr.key_value);
return acc;
}, new Set());
console.log(arr1Values);
console.log(Array.from(arr2KeyValues));
// Test if any of the values in objects in the first array are
// equal to any of the key_values in the second array
console.log(arr1Values.some(k => arr2KeyValues.has(k)));
看来您必须将一个数组中的每个对象与另一个数组中每个对象的键进行比较。最初的蛮力方法有 3 个嵌套的 for 循环:
// Loop through the objects in the first array
for (const objectA of arrayA) {
// Loop through that object's keys
for (const key in objectA) {
// Loop through the objects in the second array
for (const objectB of arrayB) {
if (objectA[key] === objectB.key_value) {
// do all the stuff
}
}
}
}
这是我最后做的,只是为了留下记录:)
let stopRules = pd.stopRules;
let pdSegs = pd.segs;
let routeStopsTest = [];
//Returns a flat array of all unique values in first object
//Thanks @slider!
let pdSegValues = Array.from(pdSegs.reduce((acc, curr) => {
Object.values(curr).forEach(v => acc.add(v));
return acc;
}, new Set()));
//Pushes all objects from stopRules array to a holding array
//When they match individual segments in the pdSegs array
pdSegValues.forEach( seg => {
let nullTest = stopRules.filter(o => o.key_value === seg);
if(nullTest.length !== 0){
routeStopsTest.push(nullTest);
}else{}
});
然后我所要做的就是展平生成的对象数组,我得到了我需要的结果,然后我可以循环遍历它以达到最初的目的。
谢谢大家的宝贵意见。我在这里学到了很多东西:)
我有两个对象数组:
[
0: {key1: value1, key2: value2, key3: value3},
1: {key1: value1, key2: value2, key3: value3}
]
[
0: {stop_id: 173, file_id: "1", key_type: null, key_value: "0020", seg_beg: 32},
1: {stop_id: 176, file_id: "1", key_type: null, key_value: "0201", seg_beg: 10},
2: {stop_id: 176, file_id: "1", key_type: null, key_value: "0201", seg_beg: 10}
]
我需要检查第一个对象中任何键的值是否与 key_value...键的任何值匹配, 在第二个对象中,然后将变量进一步设置为匹配记录中的 stop_id 值。像这样:
if(object1.value === object2.key_value){
match = object2[iterator].stop_id;
}
为了简化这一点,我试图只获取第一个对象的值:
//pd.segs is object 1
let pdSegValues = [];
for(let i=0;i<pd.segs.length;i++){
pdSegValues.push(Object.values(pd.segs[i]));
}
但这又让我得到了一个数组数组,基本上让我回到了同样的情况。我的脑子坏了,不可否认,我对循环有一个弱点。任何人都可以告诉我一个体面的方法来完成我在这里需要的东西吗?
您可以通过收集要测试的值然后使用 some
.
let arr1 = [
{"a1": "value1", "b1": "value2"},
{"a2": "0020", "b2": "value22"},
{"a3": "value111", "b3": "0201"}
];
let arr2 = [
{stop_id: 173, file_id: "1", key_type: null, key_value: "0020", seg_beg: 32},
{stop_id: 176, file_id: "1", key_type: null, key_value: "0201", seg_beg: 10},
{stop_id: 176, file_id: "1", key_type: null, key_value: "0201", seg_beg: 10}
];
// accumulate unique arr1 values to an array
let arr1Values = Array.from(arr1.reduce((acc, curr) => {
Object.values(curr).forEach(v => acc.add(v));
return acc;
}, new Set()));
// accumulate all unique arr2 "key_value"
let arr2KeyValues = arr2.reduce((acc, curr) => {
acc.add(curr.key_value);
return acc;
}, new Set());
console.log(arr1Values);
console.log(Array.from(arr2KeyValues));
// Test if any of the values in objects in the first array are
// equal to any of the key_values in the second array
console.log(arr1Values.some(k => arr2KeyValues.has(k)));
看来您必须将一个数组中的每个对象与另一个数组中每个对象的键进行比较。最初的蛮力方法有 3 个嵌套的 for 循环:
// Loop through the objects in the first array
for (const objectA of arrayA) {
// Loop through that object's keys
for (const key in objectA) {
// Loop through the objects in the second array
for (const objectB of arrayB) {
if (objectA[key] === objectB.key_value) {
// do all the stuff
}
}
}
}
这是我最后做的,只是为了留下记录:)
let stopRules = pd.stopRules;
let pdSegs = pd.segs;
let routeStopsTest = [];
//Returns a flat array of all unique values in first object
//Thanks @slider!
let pdSegValues = Array.from(pdSegs.reduce((acc, curr) => {
Object.values(curr).forEach(v => acc.add(v));
return acc;
}, new Set()));
//Pushes all objects from stopRules array to a holding array
//When they match individual segments in the pdSegs array
pdSegValues.forEach( seg => {
let nullTest = stopRules.filter(o => o.key_value === seg);
if(nullTest.length !== 0){
routeStopsTest.push(nullTest);
}else{}
});
然后我所要做的就是展平生成的对象数组,我得到了我需要的结果,然后我可以循环遍历它以达到最初的目的。
谢谢大家的宝贵意见。我在这里学到了很多东西:)