0

How would I access certain elements from object from an array list?

Say I have an array:

var test = [{"a":1, "b":2, "c":3}, {"a":4, "b":5, "c":6}];

I know that test[1] will output {"a":4, "b":5, "c":6"}. And test[1].b will output 5

How can I output {"b":2, "c":3"}, {"b":5, "c":6"} without using a for loop?

5
  • Do you want to remove a from both the objects and then log it? Commented Feb 2, 2017 at 3:33
  • @Tushar yes, I just want to output the 2, 3, 5, 6. Commented Feb 2, 2017 at 3:34
  • What is the output used for? And can that property be permanently removed? Commented Feb 2, 2017 at 3:38
  • Demo Commented Feb 2, 2017 at 3:39
  • Why would you want to avoid using a loop? Commented Feb 2, 2017 at 3:43

5 Answers 5

1

If you want to return a copy of the original array without "a" property this would work:

text.map((el)=> {
  delete el.a
  return el
})
Sign up to request clarification or add additional context in comments.

3 Comments

Worth nothing that the text array will also have the a property deleted.
Oh you're right... I guess you could do this without changing the original objects var res = text.map((el)=> Object.assign({}, {b:el.b, c:el.c})
Yeah, OP doesn't mention if it's important not to modify the original.
1

You can do like this :

text.forEach(function(row){
        delete row.a;
});

It will delete 'a' property from list of text's row.

Comments

1

Filter is the wrong tool. If you don't want to modify the original array, you can map it to a new array containing copies of the original elements with the "a" property deleted.

var text = [{"a":1, "b":2, "c":3}, {"a":4, "b":5, "c":6}];
var result = text.map(
    function(elt) {
        var copy = Object.assign({}, elt);
        delete copy.a;
        return copy;
    }
)

Comments

1

As a learning exercise you could use map to create an array of new objects which contain copies of the "b" and "c" properties found in input array elements. This example uses destructuring asignment syntax in the mapping function.

var array1 = [{"a":1, "b":2, "c":"3"}, {"a":4, "b":5, "c":6}];
    
var array2 = array1.map( obj => {var {b,c} = obj; return {b,c};});
    
 // check output
 console.log (JSON.stringify( array2))  //  [{"b":2,"c":"3"},{"b":5,"c":6}]

Note that when copying objects or arrays of objects in JavaScript, what needs to be copied and whether object references can be copied unchanged needs to be identified first. So, if you needed to copy all properties except for the "a" property, this answer would need to be modified.

Comments

0

You've got 2 options: iterate over the array using the delete operator:

var text = [{"a":1, "b":2, "c":3}, {"a":4, "b":5, "c":6}];

text.forEach(function(obj) {
  delete obj.a;  
});

console.log(text);

Or create a copy of the objects that have all but the a property:

var text = [{"a":1, "b":2, "c":3}, {"a":4, "b":5, "c":6}];

var newText = text.map(function(obj) {
  var newObj = {};
  for (var prop in obj) {
    if (prop !== 'a') {
      newObj[prop] = obj[prop];
    }
  }
  return newObj;
});

console.log(newText);

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.