3

For example I have this array, if I stringfy it it would be like this:

[{"car":"Toyota","ID":"1", "Doors": "4", "price": "20.000"},{"car":"Chevrolet","ID":"2", "Doors": "2", "price": "15.000"}]

How can I do for remove from the 2 cars: the doors and price. And only leave in the array "car" and "id"? For example:

[{"car":"Toyota","ID":"1"},{"car":"Chevrolet","ID":"2"}]

Thank you!

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
Matias Rodriguez
  • 35
  • 1
  • 1
  • 7
  • Please look at the tags before using them... It clearly says `Java (not to be confused with JavaScript or JScript)`. – Turtle Jul 03 '17 at 14:59
  • What have you tried so far? You want to modify the same array or are you ok with creating a new one? – maazadeeb Jul 03 '17 at 14:59
  • can you show us what you have done or what research have you done? – yqlim Jul 03 '17 at 14:59
  • *I have this array* where is the array in your question ? – Ravi Jul 03 '17 at 15:00
  • Possible duplicate of [How do I remove a property from a JavaScript object?](https://stackoverflow.com/questions/208105/how-do-i-remove-a-property-from-a-javascript-object) – Roberto Russo Jul 03 '17 at 15:00

7 Answers7

6

let arr = [{"car":"Toyota","ID":"1", "Doors": "4", "price": "20.000"},{"car":"Chevrolet","ID":"2", "Doors": "2", "price": "15.000"}]

let arr1 = arr.map(({car, ID}) => ({car, ID}));
let arr2 = arr.map(({Doors, price, ...remainingAttrs}) => remainingAttrs);

console.log('arr1:', arr1);
console.log('arr2:', arr2);

With ES6 syntax, you can deconstruct each object to create new one without writing a loop.

In your case, total number of fields remaining is same as the total number of deleted Following are the two approaches:

  • If less number of fields are to be preserved, then you can go with:

const arr1 = arr.map(({car, ID}) => ({car, ID}))

  • If less number of fields are to be removed, then you can go with:

const arr2 = arr.map(({Doors, price, ...remainingAttrs}) => remainingAttrs)

Parth Mansata
  • 145
  • 1
  • 7
4

You can use Array.prototype.map() to customise your result array, taking a callback function as parameter which returns a new customised object, having only car and ID properties, in each iteration.

The map() method creates a new array with the results of calling a provided function on every element in the calling array.

This is how should be your code:

var results = arr.map(function(item){
  return {car : item["car"], ID : item["ID"]}
});

Demo:

var arr = [{"car":"Toyota","ID":"1", "Doors": "4", "price": "20.000"},{"car":"Chevrolet","ID":"2", "Doors": "2", "price": "15.000"}];

var results = arr.map(function(item){
  return {car : item["car"], ID : item["ID"]}
});
console.log(JSON.stringify(results));
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
3

Adding to all other answers

Cleaner approach using ES6 syntax

var originalArray =[{"car":"Toyota","ID":"1", "Doors": "4", "price": "20.000"},{"car":"Chevrolet","ID":"2", "Doors": "2", "price": "15.000"}];

var immutableArray = originalArray.map(({Doors, price, ...rest})=> rest);

console.log(immutableArray);
yaswanthkoneri
  • 408
  • 4
  • 16
1

You must iterate over your array deleting the property on each object.

Example:

for (var i = 0; i < myArray.length; i++){
  delete myArray[i].myProperty
}
Luiz
  • 2,429
  • 8
  • 28
  • 43
1

Adding to the answers, with ES6 and to avoid mutation and some ESlint issues.

const Array = [{"car":"Toyota","ID":"1", "Doors": "4", "price": "20.000"}, 
              {"car":"Chevrolet","ID":"2", "Doors": "2", "price": "15.000"}];
const newArray = Array.map((object) => {
             const {car, ID} = object;
             return {car, ID};
});
Jtaw Cañada
  • 147
  • 1
  • 7
0

Check comment for explanation:

var array=[{"car":"Toyota","ID":"1", "Doors": "4", "price": "20.000"},{"car":"Chevrolet","ID":"2", "Doors": "2", "price": "15.000"}];

var resultArr = array.map(function(obj){
  //we take only key-value pairs we need using JS bracket notation
  return {"car":obj["car"],"ID":obj["ID"]};
});

console.log(resultArr);
Bakhtiiar Muzakparov
  • 2,308
  • 2
  • 15
  • 24
0

You would be looking for the delete operator to fully remove those properties. It could look something like this:

var array = [{"car":"Toyota","ID":"1", "Doors": "4", "price": "20.000"},{"car":"Chevrolet","ID":"2", "Doors": "2", "price": "15.000"}]

for (car of array) {
    delete(car.Doors);
    delete(car.price);
}

You could also look into using Array.splice() for faster performance on large arrays.