2

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
5
  • 1
    You are assigning payload[1], payload[2] and payload[3] when you are referring to obj.key in your map function. Thus payload["rt"] remains undefined. Commented Aug 29, 2018 at 7:29
  • what value you want to store in payload["rt"] ? Commented Aug 29, 2018 at 7:38
  • please add the wanted result as well. Commented Aug 29, 2018 at 7:41
  • @NullPointer i want payload["rt"] = 10. Commented Aug 29, 2018 at 8:03
  • btw, it's quite unclear, if the wanted keys depends on the index or on the key values. Commented Aug 29, 2018 at 8:17

7 Answers 7

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

this is helpful:). thank you so much. Just to pass the keys of 'rt','do' to the payload, it is better to map them by declaring them in another array. thats sweet :)
const mapping = [null, 'rt', 'do', 'f1']; what is null for?
4

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

Comments

2

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 ]

Comments

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

Comments

0

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;

});

Comments

0

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

1 Comment

i have defined the keys of the array payload. and i want this output payload["rt"] = 10. like this.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.