简体   繁体   English

匹配 Json 键并替换为数组中的值

[英]Match Json keys and replace with values in array

So given a simple json object所以给定一个简单的json对象

[
  {"a": 1},
  {"b": 2},
  {"c": 3},
  {"d": 4}
]

and an array like this和这样的数组

var arr = ["A", "B", "C"];

I want to write the most efficient function to match the lower case keys with their upper case counter parts and replace them with the uppercase letters, I started by turning the array into a object so I wont have to iterate over and over again, but Im stuck.我想编写最有效的函数来将小写键与其大写计数器部分匹配并用大写字母替换它们,我首先将数组转换为对象,这样我就不必一遍又一遍地迭代,但是我卡住。 Can someone please show me how they would handle this in the most efficient way?有人可以告诉我他们将如何以最有效的方式处理这个问题吗?

Without further information on your amount of keys/possible keys, trying to make a generic solution would be like this:如果没有关于您的钥匙/可能钥匙数量的更多信息,尝试制作通用解决方案将是这样的:

 var arr = [ {"a": 1}, {"b": 2}, {"c": 3}, {"d": 4} ] var match = ["A", "B", "C"]; var objMatch = {} for(var i=0;i<match.length;i++) objMatch[match[i].toLowerCase()] = match[i] // pass it to an object for efficient matching for(var i=0;i<arr.length;i++){ for(var key in arr[i]){ // check if we need to replace the key if(objMatch.hasOwnProperty(key)){ var temp = arr[i][key] // keep old value delete arr[i][key] // delete key arr[i][objMatch[key]] = temp // set new key with same old value } } } console.log(arr)

var a=[
 {"a": 1},
 {"b": 2},
 {"c": 3},
 {"d": 4}
];

var arr = ["A", "B", "C"];

Define a method in Object's prototype chain to rename Object keys:在 Object 的原型链中定义一个方法来重命名 Object 键:

Object.prototype.changeKey = function (oName, nName) {
   if (oName == nName) {
       return this;
   }
   if (this.hasOwnProperty(oName)) {
      this[nName] = this[oName];
      delete this[oName];
   }
 return this; 
};

Then find the key which matches the elements of array, then change it:然后找到与数组元素匹配的键,然后更改它:

for(var i=0;i<arr.length;i++){
   for(key in a[i]){
   if(key === arr[i].toLowerCase()){a[i].changeKey (key,arr[i])}
  }
}

Here is a fiddle这是一个小提琴

You could use an helper object and loop throu all items and all keys.您可以使用辅助对象并循环遍历所有项目和所有键。

 var arr0 = [{ "a": 1 }, { "b": 2 }, { "c": 3 }, { "d": 4 }], arr1 = ["A", "B", "C"], obj = Object.create(null); arr1.forEach(function (a) { obj[a.toUpperCase()] = a; }); arr0.forEach(function (a) { Object.keys(a).forEach(function (k) { var v = a[k]; if (k.toUpperCase() in obj) { delete a[k]; a[obj[k.toUpperCase()]] = v; } }); }); document.write('<pre>' + JSON.stringify(arr0, 0, 4) + '</pre>');

From what i understand from your question从我从你的问题中了解到

var arrayOne = [
{"a": 1},
{"b": 2},
{"c": 3},
{"d": 4}
];
var arrayTwo = ["A", "B", "C"];
var tempArray = {};
$.each(arrayOne, function(key,value){
  for(arrayOneKey in value){
    $.each(arrayTwo, function(index,vl) {
      if(vl.toLowerCase() == arrayOneKey.toLowerCase()){
        tempArray[vl]=key+1;
      }
    });
  }
});
console.log(tempArray);

Don't make things complicated.不要把事情复杂化。 Use map and return a new array:使用map并返回一个新数组:

function transform(arr, keys) {
  return arr.map(function(el) {
    var obj = {};
    var key = Object.keys(el)[0];
    var uc = key.toUpperCase();
    obj[keys.indexOf(uc) > -1 ? uc : key] = el[key];
    return obj;
  });
}

transform(input, arr);

DEMO演示

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

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