3

I have an object which is getting dynamically loaded from REST response. Now i have to convert this object into 2 arrays. 1) an array with key and values in which the value is null. 2) an array with key and values in which the value is not null.

And these both arrays, i need to display them in html with key and values in sorted order like. first the not null values and then later the null values.

object1 : {
'name' : 'xyz',
'age' : '23',
'dob' : null,
'address' : null
}

Am finding a problem while converting this object into an array! What i have been trying is

this.notNullValues = new Array<string>();
for(let obj of this.object1){
console.log(obj);
}
4
  • Put the json of object once.. and tell us from which field you want to define an array ? Commented Nov 16, 2016 at 4:43
  • "I need" is not a question. This isn't a code writing service. Provide a data sample and the code you have tried to solve this with. Take a few minutes to read How to Ask Commented Nov 16, 2016 at 4:44
  • @charlietfl sry...my bad! am very new to the stack overflow.! And i will update my question with the code. Commented Nov 16, 2016 at 4:46
  • @ManishSingh, I just updated! Please have a look. Commented Nov 16, 2016 at 4:57

2 Answers 2

1

You can have a look at the below code. Also you can do the same with Object.keys instead of for..in.

Also, you can check like this, if(object1[key] == null)

var object1 = {
'name' : 'xyz',
'age' : '23',
'dob' : null,
'address' : null
};


var nullArray = [];
var notNullArray = [];

for(let key in object1){
  var item = {};
  item[key] = object1[key];
  
  if(object1[key]){
    notNullArray.push(item);
  } else {
    nullArray.push(item);
  }
}

console.log(nullArray);
console.log(notNullArray);

With Object.keys,

var object1 = {
    'name' : 'xyz',
    'age' : '23',
    'dob' : null,
    'address' : null
    };


    var nullArray = [];
    var notNullArray = [];

    Object.keys(object1).forEach(function(key) {
  var item = {};
  item[key] = object1[key];
  
  if(object1[key]){
    notNullArray.push(item);
  } else {
    nullArray.push(item);
  }
});

    console.log(nullArray);
    console.log(notNullArray);

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

5 Comments

Thanks for your time mam! But the major missing was the typescript!
This code converting into typescript. It worked!! (Y) Thanksss a lot!
could you also help me one thing with typescript too? var item = {}; am not able to get this sentence done in typescript.
@RakeshChandra Yes sure
@RakeshChandra Can you try this, var item: {};
0
    var notNullValues = new Array();
    var nullValues = new Array();
    for(let key in object1){
    var value = object1[key];
    if(value ===null){
      nullValues.push({
        key: value;
      });
    } else {
      notNullValues.push({
          key: value;
      });
    }
});

Hope it helps :)

2 Comments

Sorry! But what i wanted was in typescript! :(
Ok. i will change it to typescript !! :D

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.