I have an array called x
. It contains objects with three fields. How can I make another array
called xmini
that is the same as x
but with the field called c
missing?
var x = [
{ a : 1, b : 2, c : 3 },
{ a : 3, b : 4, c : 5 }
];
I have an array called x
. It contains objects with three fields. How can I make another array
called xmini
that is the same as x
but with the field called c
missing?
var x = [
{ a : 1, b : 2, c : 3 },
{ a : 3, b : 4, c : 5 }
];
This method will not affect the original x
object.
var xmini = [],
tmpObj = {};
for (var i = 0; i < x.length; i++) {
for (var key in x[i]) {
if (key !== 'c') {
tmpObj[key] = x[i][key];
}
}
xmini.push(tmpObj);
tmpObj = {};
}
You should try to be more specific about what you have tried and what you are trying to accomplish. Another valid answer to your question (as you phrased it) would be simply: var xmini = [{a = 1, b = 2}, {a = 3, b = 4}];
, but I assumed you wanted a function to create this new object programmatically without changing the original object and without needing to know anything about the other properties of the original object.