简体   繁体   English

比较2个数组并替换匹配的键中的值?

[英]Compare 2 arrays and replace values from keys that match?

Heres my attempt. 这是我的尝试。

Problems: Currently my return ds1.locale = dataSrc2[i][property] line is a point of failure. 问题: 当前,我的return ds1.locale = dataSrc2[i][property]行是一个故障点。 I know map returns a new array; 我知道map返回一个新数组; however, I want the properties and values of the original dataSrc1 with exception to the value of ds1.locale . 不过,我想在原有的属性和值dataSrc1有例外的价值 ds1.locale

Question: How can I return an array while carry over the original other key value pairs from dataSrc1 with exception to dataSrc1.locale which value is replaced with the matching dataSrc2 key's value. 问题:我如何在将原始其他键值对从dataSrc1dataSrc1.locale该值被匹配的dataSrc2键的值替换)例外的同时返回数组。

UPDATE I solved it. 更新我解决了。 But the code is really ugly. 但是代码确实很难看。 Is there a better approch to this? 有更好的办法吗? perhaps not using 3 damn loops? 也许不使用3个该死的循环?

Here's pseudo code of the steps. 这是步骤的伪代码。

//1. loop over dataSrc1.
//2. loop over dataSrc2.
//3. try find a match from dataSrc2[key] e.g. dataSrc2['af'] === dataSrc1.locale;
//4. if matched save dataSrc2's key
//5. replace dataSrc1.language = dataSrc2[savedDataSrc2Key]

var dataSrc1 = [{'locale': 'af', 'language': 'Afrikaans'}, {'locale': 'ar', 'language': 'Arabic'}];
var dataSrc2 = [{'ar': '丹麥文'},{'af': '土耳其文'}];
//Intended output
//dataSrc3 = [{'locale': 'af', 'language': '土耳其文'}, {'locale': 'ar', 'language': '丹麥文'}]

Repl Code 复制代码

var dataSrc3 = dataSrc1.map(function(ds1){
    for(var i = 0; i < dataSrc2.length; i += 1){

        for (var property in dataSrc2[i]) {
            if (dataSrc2[i].hasOwnProperty(property)) {
                if(property === ds1.locale){
                    ds1.language = dataSrc2[i][property];
                    return ds1; 
                }
            }
        }
    }
})
console.log(dataSrc3);

//Current output
//[ '土耳其文', '丹麥文' ]

//Intended output
//dataSrc3 = [{'locale': 'af', 'language': '土耳其文'}, {'locale': 'ar', 'language': '丹麥文'}]

You can refactor a little bit: 您可以重构一下:

var dataSrc3 = dataSrc1.map(function(d1) {
  var language = null;
  // .some will iterate until you return true or last item is passed
  // set variable language to found language
  dataSrc2.some(function(d) {
    if (Object.prototype.hasOwnProperty.call(d, d1.locale)) {
      language = d[d1.locale];
      return true;
    }
  });
  // return a new object, this will not modify the objects in dataSrc1 and dataSrc2
  return { language: language, locale: d1.locale };
});
console.log(dataSrc3); // [{'locale': 'af', 'language': '土耳其文'}, {'locale': 'ar', 'language': '丹麥文'}]

There is an experimental array method called .find that works sort of like .some but will give you the current value in the array: 有一个名为.find的实验性数组方法,其工作方式类似于.some但会为您提供数组中的当前值:

var dataSrc3 = dataSrc1.map(function(d1) {
  var d2 = dataSrc2.find(function(d2) {
    return Object.prototype.hasOwnProperty.call(d2, d1.locale);
  });
  // return a new object, this will not modify the objects in dataSrc1 and dataSrc2
  return { 
    language: d2[d1.locale], 
    locale: d1.locale 
  };
});
console.log(dataSrc3); // [{'locale': 'af', 'language': '土耳其文'}, {'locale': 'ar', 'language': '丹麥文'}]

You might wanna take a look at underscore.js or lodash . 您可能想看看underscore.jslodash These libraries will provide usefull util functions that can be used for older browser as well: 这些库将提供有用的util函数,这些函数也可用于较旧的浏览器:

var dataSrc3 = _.map(dataSrc1, function(d1) {
  var d2 = _.find(dataSrc2, function(d2) {
    return _.has(d2, d1.locale);
  });
  return { 
    language: d2[d1.locale], 
    locale: d1.locale 
  };
});

There are two mistakes in our code: 我们的代码中有两个错误:

return ds1.locale = dataSrc2[i][property];

This assigns to and returns ds1.locale , while you want to assign to ds1.language , and return the modified ds1 : 这将分配并返回ds1.locale ,而您想分配给ds1.language ,并返回修改后的ds1

ds1.language = dataSrc2[i][property];
return ds1;

Secondly, you should also return any unmodified ds1 , so add after the for loop: 其次,您还应该返回任何未修改的ds1 ,因此请在for循环之后添加:

return ds1;

As others have indicated, you can write the function more concisely. 正如其他人指出的那样,您可以更简洁地编写该函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM