如何在共享相同未知密钥的同一数组中合并多个对象?

时间:2019-08-16 01:01:46

标签: javascript

我正在尝试将多个对象合并到一个数组中,这些对象共享键,但是这些键是未知的。例如

var obj = {
    "127.0.0.1:26969" : 1,
    "127.0.0.2:26969" : 1,
    "127.0.0.3:26969" : 1,
    "127.0.0.4:26969" : 1,
}

var obj1 = {
    "127.0.0.1:26969" : 1001,
    "127.0.0.2:26969" : 1002,
    "127.0.0.3:26969" : 1003,
    "127.0.0.4:26969" : 10004,
}

var obj2 = {
    "127.0.0.1:26969" : 2001,
    "127.0.0.2:26969" : 2002,
    "127.0.0.3:26969" : 2003,
    "127.0.0.4:26969" : 2005,
}

The desired output is to be like this

var array = [
  {
   "ip": "127.0.0.1",
   "status": "local" /// this is custom key it didn't exist in the original obj
  },{
   "ip": "127.0.0.2",
   "status": "local-local",
   "first-customkey": 1001,
   "second-customkey": 2001
  },etc.....
];

到目前为止,我一直尝试着组成包含这些对象的数组

通过这种方法

var  objCombine = [];
for (let key in obj) {
  objCombine.push({
    'ip': key.split(':')[0],
    'status': 'local',
  })                      
}

for (let key in obj1) {
  objCombine.push({
    'ip': key.split(':')[0],
    'first-customkey': obj1[key],
  })                      
}

for (let key in obj2) {
  objCombine.push({
    'ip': key.split(':')[0],
    'second-customkey': obj2[key],
  })                      
}

输出类似于期望的结果,但是我现在有一个数组,其中包含多个具有未知共享密钥的对象,这是如何将每个对象与其键对合并的问题。

现在我已经尝试过

function extend (array) {
    var newArray = [];
    var extended = {};
    var merge = function (obj, callback) {
      for (var prop in obj) {
        if (obj.hasOwnProperty(prop)) {
           extended[prop] = obj[prop];
        }
      }
      callback(extended);
    };
    array.forEach(arg => merge(arg, callback));
    function callback (extended) {
        if (Object.keys(extended).length === 4) {
            console.log(extended);
            newArray.push(extended);
        }
    }
    return newArray;
};

extend(objCombine);

但是我只能像这样重复最后一个对象

[
  {
    "ip": "127.0.0.4",
    "status": "local",
    "first-customkey": 10004,
    "second-customkey": 2005
  },
  {
    "ip": "127.0.0.4",
    "status": "local",
    "first-customkey": 10004,
    "second-customkey": 2005
  },
  {
    "ip": "127.0.0.4",
    "status": "local",
    "first-customkey": 10004,
    "second-customkey": 2005
  },
  {
    "ip": "127.0.0.4",
    "status": "local",
    "first-customkey": 10004,
    "second-customkey": 2005
  }
]

最后一个对象重复4次。

注意状态:“本地”,我想在每个对象中重复。就像上面的是静态值一样。

PS 该示例中提供的所有数据不是真实的实时数据,仅用于解释我的示例。

1 个答案:

答案 0 :(得分:1)

也许这会对您有所帮助。无法获得确切的属性名称。

var obj = {
    "127.0.0.1:26969" : 1,
    "127.0.0.2:26969" : 1,
    "127.0.0.3:26969" : 1,
    "127.0.0.4:26969" : 1,
}

var obj1 = {
    "127.0.0.1:26969" : 1001,
    "127.0.0.2:26969" : 1002,
    "127.0.0.3:26969" : 1003,
    "127.0.0.4:26969" : 10004,
}

var obj2 = {
    "127.0.0.1:26969" : 2001,
    "127.0.0.2:26969" : 2002,
    "127.0.0.3:26969" : 2003,
    "127.0.0.4:26969" : 2005,
}

// easy to loop through objects in array.
const objArr = [obj, obj1, obj2];
// getting object keys to easily find each value in each object.
const objKeys = Object.keys(obj);
// array to store the new array objects 
const array = [];
// looping through each key
objKeys.forEach((key) => {
  // creating the desired object.
  const newObj = {
      ip: key,
      status: "local-local",
    }
  // looping through each object and getting the value of that key and putting it on the new object
  objArr.forEach((obj, index) => {
    newObj[`customkey-${index}`] = obj[key];
  })
  // pushing that object once all values have been added
  array.push(newObj);
})

console.log(array);