I have an example:
var A = [{
key: "iphone",
value: "American"
}, {
key: "sony",
value: "Japan"
}]
I want to do this action:
B=[{value:"American"},{value:"Japan"}]
How can I do this? Help me.
I have an example:
var A = [{
key: "iphone",
value: "American"
}, {
key: "sony",
value: "Japan"
}]
I want to do this action:
B=[{value:"American"},{value:"Japan"}]
How can I do this? Help me.
Use Array.map
and return a new Object with the value field,
DEMO
var A =[{key:"iphone",value:"American"},{key:"sony",value:"Japan"}] ;
var result = A.map(d => ({ value: d.value }));
console.log(result);
var B = A.map(function(obj) { return { value: obj.value }; });
or
var B = A.map(obj => ({ value: obj.value }));
Maybe you prefer this more readable syntax?
function myObjectConverter (inputObject) {
var outputObject = {};
// ignore the key property
outputObject.Country1 = inputObject.value1;
outputObject.Country2 = inputObject.value2;
outputObject.Country3 = inputObject.value3;
outputObject.Country4 = inputObject.value4;
// transfer any other items with new names
return outputObject;
}
var B = A.map(myObjectConverter);