【问题标题】:Match values from one object to another将一个对象的值匹配到另一个对象
【发布时间】:2018-10-16 00:07:02
【问题描述】:

我有两个对象数组:

[ 
  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]));
}

但这又让我得到了一个数组数组,基本上让我回到了同样的境地。我患有脑筋急转弯,并且无可否认地有循环的弱点。谁能告诉我一个体面的方法来完成我在这里需要的东西?

【问题讨论】:

  • and then do something 这可能很重要——您是否需要对匹配的对象进行操作,还是只需要知道是否存在匹配项?第一个产生简单的代码,第二个产生非常复杂的代码
  • 我需要设置一个变量,进一步向上等于匹配记录的 stop_id 值
  • 相应地编辑了问题:)
  • 您是否只需要一个是/否布尔值来判断匹配是否存在?
  • @mark meyer,不,我需要设置一个更高的变量,直到匹配记录的 stop_id 值。请参阅上面编辑过的问题

标签: javascript arrays json for-loop key-value


【解决方案1】:

您可以通过收集要测试的值然后使用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)));

【讨论】:

  • 这很有趣,我会测试它并返回
  • 这是一个非常好的解决方案,而且比我尝试过的任何方法都要简洁。谢谢!
【解决方案2】:

看起来您必须将一个数组中的每个对象与另一个数组中每个对象的键进行比较。最初的蛮力方法有 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
      }
    }
  }
}

【讨论】:

  • 这就是我所担心的,尽管您的回答中有一些我没有想到的事情。我会给他们一个机会
【解决方案3】:

这就是我最终要做的,只是为了留下记录:)

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{}
});

然后我所要做的就是将生成的对象数组展平,然后我就有了我需要的结果,然后我可以循环遍历以达到最初的目的。

感谢大家的深刻见解。我在这里学到了很多东西:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-24
    • 2016-06-24
    • 2019-12-27
    • 2011-02-07
    • 2013-05-07
    • 2016-06-13
    • 1970-01-01
    • 2012-04-03
    相关资源
    最近更新 更多