0

I have an array of objects. if i do a console.log, i can see these array of objects.

[Object, Object, Object, Object,Object]
[0-4]
0:Object
    Name: Nikhil
    User_ID:123 
    admin:true
    read:false
    write:false
1:Object
    Name:andy
    User_ID:124
    admin:false
    read:true
    write:false
2:Object
    Name:Nik
    User_ID:125
    admin:false
    read:false
    write:true
3:Object
    Name:ranea
    User_ID:126
    admin:false
    read:false
    write:true 
4:Object
    Name:isha
    User_ID:127
    admin:false
    read:true
    write:false

Now, if i do JSON.stringify, i get this output.

[{"Name":"Nikhil","User_ID":"123","admin":true,"read":false,"write":false},
{"Name":"andy","User_ID":"124","admin":false,"read":true,"write":false},
{"Name":"Nik","User_ID":"125","admin":false,"read":false,"write":true},         
{"Name":"ranea","User_ID":"126","admin":false,"read":false,"write":true},
{"Name":"isha","User_ID":"127","admin":false,"read":true,"write":false}]

Instead of doing stringify to all the parameters, I want to only do it to few. For e.g. I don't want to pass Name. I only want to pass User_ID since it is unique, and admin,read,write properties.

How can i create a new array of objects using loadash and then stringify the result. My final output after JSON.stringify should be like this

[{"User_ID":"123","admin":true,"read":false,"write":false},
{"User_ID":"124","admin":false,"read":true,"write":false},
{"User_ID":"125","admin":false,"read":false,"write":true},         
{"User_ID":"126","admin":false,"read":false,"write":true},
{"User_ID":"127","admin":false,"read":true,"write":false}]

3 Answers 3

3

Using _.pick it should be:

 var newArr = _.map(oldArray,
  function(item) {
    return _.pick(item, ['User_ID', 'admin', 'read', 'write']);
  });
Sign up to request clarification or add additional context in comments.

2 Comments

Hi @theodore- i am getting undefined for all the values. can you have a look at my console output which is mentioned at the start of question.
Hello @anjaligupte checkout my edited answer. I had forgotten the return statement.
2

You could make use of the Array.map() function.

var obj = [ { "Name": "Christophe", "Age": 42, "foo": "bar" }, { "Name": "Blah", "Age": 42, "foo": "foo2" }]
var filtered = obj.map(function (element) {
    return {
        "Name": element.Name
    }
});

After that, filtered contains your objects with only the keys you want to keep, and you can JSON.stringify it.

console.log(JSON.stringify(filtered));
// [ {"Name": "Christophe"}, {"Name": "Blah"} ]

Comments

0

You can use this little function to return the new Array.

var arr = [{
  Name: "Nikhil",
  User_ID: "123",
  admin: "true",
  read: "false",
  write: "false"
}, {
  Name: "andy",
  User_ID: "124",
  admin: "false",
  read: "true",
  write: "false"
}];

function returnArr(array) {
  var arrNew = arr;
  for (let in arrNew) {
    delete arrNew[let].Name;
  }
  return JSON.stringify(arrNew);
}

document.write(returnArr(arr)); // or console.log(returnArr(arr))

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.