codemt
codemt

Reputation: 481

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

Upvotes: 2

Views: 19112

Answers (7)

Andrey Davydov
Andrey Davydov

Reputation: 91

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);

Upvotes: 4

Nina Scholz
Nina Scholz

Reputation: 386868

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; }

Upvotes: 4

RAJA
RAJA

Reputation: 144

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

Upvotes: 0

zoey0_0
zoey0_0

Reputation: 36

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 ]

Upvotes: 2

Manasi
Manasi

Reputation: 765

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"];

Upvotes: 0

Muhammad Osama
Muhammad Osama

Reputation: 1047

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;

});

Upvotes: 0

Eriks Klotins
Eriks Klotins

Reputation: 4180

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: ""}

Upvotes: 0

Related Questions