-5

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.

Obsidian Age
  • 41,205
  • 10
  • 48
  • 71
bechin
  • 1
  • 2
  • 3
    Welcome to StackOverflow! Have you tried anything so far? StackOverflow isn't a free code-writing service, and expects you to [**try to solve your own problem first**](http://meta.stackoverflow.com/questions/261592). Please update your question to show what you have already tried, showcasing a **specific** problem you are facing in a [**minimal, complete, and verifiable example**](http://stackoverflow.com/help/mcve). For further information, please see [**how to ask good questions**](http://stackoverflow.com/help/how-to-ask), and take the [**tour of the site**](http://stackoverflow.com/tour). – Obsidian Age Feb 23 '18 at 01:41
  • 1
    Possible duplicate of [How to remove properties from an object array?](https://stackoverflow.com/questions/37222885/how-to-remove-properties-from-an-object-array) – Sebastian Simon Feb 23 '18 at 02:15

3 Answers3

2

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);
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • Using a single liner implied return would be preferable here – Sterling Archer Feb 23 '18 at 01:50
  • var A = [{ key: "iphone", value1: "American1", value2:""American2", value3:""American3", value4:""American4", }, { key: "sony", value1: "japan1", value2:""japan2", value3:""japan3", value4:""japan4", }] ==> B = [{ value1: "American1", value2:""American2", value3:""American3", value4:""American4", }, { value1: "japan1", value2:""japan2", value3:""japan3", value4:""japan4", }] can i delete "key" – bechin Feb 23 '18 at 01:54
1

var B = A.map(function(obj) { return { value: obj.value }; });

or

var B = A.map(obj => ({ value: obj.value }));

P.S.
  • 15,970
  • 14
  • 62
  • 86
Raith
  • 528
  • 3
  • 8
0

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);
Raith
  • 528
  • 3
  • 8