0

I have this following array of Objects.

[Object, Object, Object]
    0:Object
       name: "Rick"
       Contact: "Yes"
       id: 1
    1:Object
       name:"Anjie"
       Contact:"No"
       id: 2
    2:Object
       name:"dillan"
       Contact:"Maybe"
       id:3

Now, i want only the name and Contact objects out of it. How can i get that. Also, in other case, i want name and id. Can someone let me know how to achieve this please.

for e.g. only name and Contact should give this result.

[Object, Object, Object]
    0:Object
      name: "Rick"
      Contact: "Yes"
    1:Object
      name:"Anjie"
      Contact:"No"
    2:Object
      name:"dillan"
      Contact:"Maybe"
3
  • 1
    What don't you know how to do? Seems like a pretty simple problem. Where are you stuck? Commented Jul 27, 2016 at 19:38
  • 1
    This previous question of yours gets you much of the way there. Commented Jul 27, 2016 at 19:40
  • name and Contact are properties of the Object. They are Strings, which are of course objects in themselves. But it would have been clearer to describe them as properties. Anyway you can use Array.map to achieve this Commented Jul 27, 2016 at 20:41

5 Answers 5

1

The simplest way is to use the map function:

var objs = objs.map(function(obj) {
    return {
        name: obj.name,
        Contact: obj.Contact
    }
});

Alternatively, you can loop through it manually:

var objs = [{
       name: "Rick",
       Contact: "Yes",
       id: 1
}, {
       name:"Anjie",
       Contact:"No",
       id: 2
}, {
       name:"dillan",
       Contact:"Maybe",
       id:3
}];

var newObjs = [];

for ( var i=0, len = objs.length; i < len; i++ )
{
    newObjs.push({
        name: objs[i].name,
        Contact: objs[i].Contact
    });
}

console.log(newObjs);

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

Comments

1

If you want to access some properties of an object in array than simply you can use a for loop and passing value of i like:-

for(var i=0;i<object.length;i++){
   object[i].name // give you name
   object[i].contact // give you contact do whatever you want to do with this
}

or simply write a function that make a separate object from your object in array.

Comments

1

You could use underscore's map and pick:

var result = _.map(data, item => _.pick(item, 'name', 'Contact'));  

Comments

1

A generic solution coould be one with a given data array and keys array. The wanted properties are mapped in a new array.

function getWithKeys(array, keys) {
    return array.map(function (a) {
        var o = {};
        keys.forEach(function (k) {
            o[k] = a[k];
        });
        return o;
    });
}

var array1 = [{ name: "Rick", Contact: "Yes", id: 1 }, { name: "Anjie", Contact: "No", id: 2 }, { name: "dillan", Contact: "Maybe", id: 3 }],
    array2 = getWithKeys(array1, ['name', 'Contact']),
    array3 = getWithKeys(array1, ['name', 'id']);

console.log(array2);
console.log(array3);

Comments

0

You could use map() and return array of new objects with deleted property. To create copy of object you can use (JSON.parse(JSON.stringify()))

var data = [{ id: 1, name: "Rick", Contact: "Yes" }, { id: 2, name: "Anjie", Contact: "No" }, { id: 3, name: "dillan", Contact: 'Maybe' }];

var result = data.map(function(e) {
  var r = (JSON.parse(JSON.stringify(e)));
  delete r.id;
  return r;
});

console.log(result)
console.log(data)

Or you an use Object.assign({}, e)

var data = [{ id: 1, name: "Rick", Contact: "Yes" }, { id: 2, name: "Anjie", Contact: "No" }, { id: 3, name: "dillan", Contact: 'Maybe' }];

var result = data.map(function(e) {
  var r = Object.assign({}, e);
  delete r.id;
  return r;
});

console.log(result);
console.log(data);

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.