简体   繁体   中英

How to Map Array values from one Array to Another Array JavaScript?

This is my Code. Where I want to Pass the Values of kvArray to Second Array.

var kvArray = [{key: 1, value: 10}, 
               {key: 2, value: 20}, 
               {key: 3, value: 30}];

var reformattedArray = kvArray.map(obj => { 
   var payload = {};
   payload["rt"];
   payload["do"];
   payload["f1"];
   payload[obj.key] = obj.value;
   console.log(payload["rt"]);
   return payload;
});

The console.log is coming undefined . Can anyone help here? I am pretty new to Map function.

I want to Print this result.

 payload["do"]=10
 payload["f1"]=20
 payload["f2"]=30

 var kvArray = [{key: 1, value: 10}, {key: 2, value: 20}, {key: 3, value: 30}]; var reformattedArray = kvArray.map(obj =>{ var payload = {}; const mapping = [null, 'rt', 'do', 'f1']; const key = mapping[obj.key]; payload[key] = obj.value; return payload; }); console.log(reformattedArray); 

You could use a destructuring assignment and build a new object with computed property names .

For the final keys, you could use an object keys with corresopnding keys to the the keys of the given object.

 var kvArray = [{ key: 1, value: 10 }, { key: 2, value: 20 }, { key: 3, value: 30 }], keys = { 1: 'rt', 2: 'do', 3: 'fi' }, result = kvArray.map(({ key, value }) => ({ [keys[key]]: value })); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

Not sure what you are trying to achieve with lines:

payload["rt"];
payload["do"];
payload["f1"];

If you want to create new keys in the reformatterArray, try assigning a value, eg.

var kvArray = [{key: 1, value: 10}, 
           {key: 2, value: 20}, 
           {key: 3, value: 30}];
var reformattedArray = kvArray.map(obj =>{ 
   var payload = {};
   payload["rt"] = "";
   payload["do"]= "";
   payload["f1"]= "";
   payload[obj.key] = obj.value;
   console.log(payload["rt"]);
   return payload;
});

console.log(reformattedArray):
//result
0: {1: 10, rt: "", do: "", f1: ""}
1: {2: 20, rt: "", do: "", f1: ""}
2: {3: 30, rt: "", do: "", f1: ""}

You were assigning the value to payload but getting the values of payload.

  var kvArray = [{key: 1, value: 10}, {key: 2, value: 20}, {key: 3, value: 30}]; var reformattedArray = kvArray.map(obj =>{ var payload = {}; payload[obj.key] = obj.value; console.log(payload[obj.key]); return payload; }); 

You are mapping data to reformattedArray , so you need to pring reformattedArray to get values,

  var kvArray = [{key: 1, value: 10}, {key: 2, value: 20}, {key: 3, value: 30}]; var reformattedArray = kvArray.map(obj =>{ var payload = {}; payload[obj.key] = obj.value; return payload; }); console.log(reformattedArray); 

Also you have following code which is of no use

payload["rt"]; payload["do"]; payload["f1"];

I'm not sure what format you want.

try the code below:

var kvArray = [{ key: 1, value: 10 },
  { key: 2, value: 20 },
  { key: 3, value: 30 }];
var reformattedArray = kvArray.map(obj => obj.value);
console.log(reformattedArray)

the result will be:

[ 10, 20, 30 ]

This code works fine

let kvArray = [{key: 1, value: 10},
           {key: 2, value: 20},
           {key: 3, value: 30}];

let reformattedArray = kvArray.map(obj =>{
   var payload = {};
   payload[obj.key] = obj.value;
   return payload;
});

console.log(reformattedArray)

The above code print output like this

[
  0: {1: 10}
  1: {2: 20}
  2: {3: 30}
]

For more info, refer this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM