Karan
Karan

Reputation: 1118

Change the order of JSON object Array

I am a beginner in Javascript. I am creating a project using Javascript that has two JSON arrays:

 var interfaces = [
                {id:'123123-12-31-23',mac:'234',flowid:'1',status:'deployed'},
                {id:'456456-12-31-23',mac:'45645',flowid:'2',status:'deployed'},
                {id:'123123-12-31-234',mac:'45',flowid:'3',status:'deployed'},
                {id:'456456-12-31-234',mac:'89',flowid:'4',status:'deployed'}
            ];

    var reorderedInterfaces =   [

                {id:'456456-12-31-23',mac:'45645',flowid:'2',status:'deployed'},
                {id:'456456-12-31-234',mac:'89',flowid:'4',status:'deployed'},
                {id:'123123-12-31-234',mac:'45',flowid:'3',status:'deployed'},
                {id:'123123-12-31-23',mac:'234',flowid:'1',status:'deployed'},
            ];

I need the following output:

var finalOutput = {
    1:2,
    2:4,
    3:3,
    4:1
}

Here is my code:

var obj = {}
    for(i=0; i<interfaces.length; i++){
        console.log()
        for(j=0; j<reorderedInterfaces.length; j++){
            obj[interfaces[i].flowid] = reorderedInterfaces[j].flowid
            break;
        }

    }   
    console.log(obj)

This gives me wrong output

Upvotes: 0

Views: 58

Answers (3)

Ramya
Ramya

Reputation: 42

Change your code from

var obj = {}
    for(i=0; i<interfaces.length; i++){
        console.log()
        for(j=0; j<reorderedInterfaces.length; j++){
            obj[interfaces[i].flowid] = reorderedInterfaces[j].flowid
            break;
        }

    } 

to

var obj = {}
    for(i=0; i<interfaces.length; i++){
        for(j=0; j<reorderedInterfaces.length; j++){
            if(i==j){
               obj[interfaces[i].flowid] = reorderedInterfaces[j].flowid;
            }
        }

    } 

for the expected output. Hope this helps you

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386604

You need just a single loop and take the same index.

var interfaces = [{ id: '123123-12-31-23', mac: '234', flowid: '1', status: 'deployed' }, { id: '456456-12-31-23', mac: '45645', flowid: '2', status: 'deployed' }, { id: '123123-12-31-234', mac: '45', flowid: '3', status: 'deployed' }, { id: '456456-12-31-234', mac: '89', flowid: '4', status: 'deployed' }],
    reorderedInterfaces = [{ id: '456456-12-31-23', mac: '45645', flowid: '2', status: 'deployed' }, { id: '456456-12-31-234', mac: '89', flowid: '4', status: 'deployed' }, { id: '123123-12-31-234', mac: '45', flowid: '3', status: 'deployed' }, { id: '123123-12-31-23', mac: '234', flowid: '1', status: 'deployed' }, ],
    result = {};

interfaces.forEach(function (a, i) {
    result[a.flowid] = reorderedInterfaces[i].flowid;
});

console.log(result);

Upvotes: 1

Deep
Deep

Reputation: 9794

you just need assign the values when the iterator i and j have same values.

if(i == j)
{
  obj[interfaces[i].flowid] = reorderedInterfaces[j].flowid
  break;
}

var interfaces = [{
  id: '123123-12-31-23',
  mac: '234',
  flowid: '1',
  status: 'deployed'
}, {
  id: '456456-12-31-23',
  mac: '45645',
  flowid: '2',
  status: 'deployed'
}, {
  id: '123123-12-31-234',
  mac: '45',
  flowid: '3',
  status: 'deployed'
}, {
  id: '456456-12-31-234',
  mac: '89',
  flowid: '4',
  status: 'deployed'
}];

var reorderedInterfaces = [

  {
    id: '456456-12-31-23',
    mac: '45645',
    flowid: '2',
    status: 'deployed'
  }, {
    id: '456456-12-31-234',
    mac: '89',
    flowid: '4',
    status: 'deployed'
  }, {
    id: '123123-12-31-234',
    mac: '45',
    flowid: '3',
    status: 'deployed'
  }, {
    id: '123123-12-31-23',
    mac: '234',
    flowid: '1',
    status: 'deployed'
  },
];


var obj = {}
for (i = 0; i < interfaces.length; i++) {
  for (j = 0; j < reorderedInterfaces.length; j++) {
    if(i == j)
    {
    obj[interfaces[i].flowid] = reorderedInterfaces[j].flowid
    break;
    }
  }

}
console.log(obj)

Upvotes: 3

Related Questions