sasha romanov
sasha romanov

Reputation: 485

how to merge multiple objects in the same array that share the same unknown key?

I'm trying to merge multiple objects in one array these objects share keys , but these keys are unknown. for example

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.....
];

what i've tried till now is to compose the array containing these objects

through this method

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],
  })                      
}

The output was similar to the desired result , but i have now an array that contains multiple objects that has unknown shared keys, the question how to merge each object with the its key pair.

Now i've tried this

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

but i get only the last object repeated like this

[
  {
    "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
  }
]

the last object is repeated 4 times.

NOTE status: "local", i want to be repeated in each object. just like above it's static values.

PS all data that is provided in the example is not real live data it's just to explain my example.

Upvotes: 1

Views: 276

Answers (1)

tytyf
tytyf

Reputation: 342

maybe this will help you. couldn't get the exact property names.

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



Upvotes: 1

Related Questions