-1

Input:

var npi = {'test1':{'address':'','num':'12'},'test2':{'address':'','num':'12'},'test3':{'address':'cleveland','num':'12'},'test4':{'address':'hostun','num':'12'}}

Expected output:

var array = ['cleaveland','hostun']

i.e push only if an address is available.

My code:

for(var i = 0;i < = 4;i++){
    if(npi.test+''+i.address) {
        array.push(npi.test+''+i.address);
    }
}

But it is not working since I did a mistake, can anyone please help me? Thanks.

BhalchandraSW
  • 714
  • 5
  • 13
Duster
  • 99
  • 3
  • 9
  • `npi.test` = undefined. then if you do `+ ' ' + i.address` you end up with `if ("undefined undefined") { ... }` – mhodges Jun 23 '17 at 15:05
  • Possible duplicate of [Dynamically access object property using variable](https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable) – mhodges Jun 23 '17 at 15:17

5 Answers5

2

You can get the keys of the npi object, filter them to only get ones that are 'testN' (where N is any number) and that have an .address that is not blank, then map that:

var npi = {'not':{},'nsi':{}, 'test1':{'address':'','num':'12'},'test2':{'address':'','num':'12'},'test3':{'address':'cleveland','num':'12'},'test4':{'address':'hostun','num':'12'}}

var array = Object.keys(npi)
  .filter(function(k) { return /^test\d+$/.test(k) && npi[k].address })
  .map(function(k) { return npi[k].address })

console.log(array)

Further reading:

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
0

Try this:

var objNPI = Json.parse(npi); for(var i = 0;i < = 4;i++){ var prop = 
objNPI[i].address; if(prop) { array.push(objNPI); } }
0

You can do it using Object.keys()

var npi = {
    'test1': {
        'address': '',
        'num': '12'
    },
    'test2': {
        'address': '',
        'num': '12'
    },
    'test3': {
        'address': 'cleveland',
        'num': '12'
    },
    'test4': {
        'address': 'hostun',
        'num': '12'
    }
};

var addressArr = [];

var objKeys = Object.keys(npi);
for (var i = 0; i < objKeys.length; i++) {
    if (npi[objKeys[i]].address) {
        addressArr.push(npi[objKeys[i]].address);
    }
}

console.log(addressArr)
Mohamed Abbas
  • 2,228
  • 1
  • 11
  • 19
0

This code will solve your problem:

var array = [];
var keys = Object.keys(npi).slice();
for(key of keys){
  if(npi[key].address != ''){
    array.push(npi[key].address);
  }
}
Eduardo Melo
  • 481
  • 1
  • 6
  • 19
  • npi = {'not':{},'nsi:{},'test1':{'address':'','num':'12'},'test2':{'address':'','num':'12'},'test3':{'address':'cleveland','num':'12'},'test4':{'address':'hostun','num':'12'}} – Duster Jun 23 '17 at 15:12
  • i need to consider test only – Duster Jun 23 '17 at 15:13
0

The issue is how you are accessing your properties in your object. You need to use bracket notation for accessing properties dynamically, like so.

var npi = {'test1':{'address':'','num':'12'},'test2':{'address':'','num':'12'},'test3':{'address':'cleveland','num':'12'},'test4':{'address':'hostun','num':'12'}};

var array = []

for(var i = 1;i <= 4;i++){
  // access npi test properties dynamically with bracket [] syntax
  var address = npi["test"+i].address;
  if(address) {
    array.push(address);
  }
}

console.log(array);
mhodges
  • 10,938
  • 2
  • 28
  • 46