0

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

2
  • What happens exactly with your code? Where is your problem? Commented Jan 18, 2017 at 12:56
  • 1
    They are not JSON. They are Javascript arrays. Commented Jan 18, 2017 at 12:56

3 Answers 3

3

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)

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.