-4

I am trying to add key value pair in the below JSON object, but it is getting added in the wrong way, is there any way i can add to each object a new key value pair, that will help me to complete the req.

JSON as follow :

var object =[
      {
         "asdf":"",
         "fdrtf":"869966",
         "hdhfhfh":"utytut",
         "Cat":"A"
         
      },
      {
         "hjhj":"",
         "hfhfhf":"h",
         "hhfh":"hfjfhdj",
         "cat":"B"
         
      },
      {
         "hjhj":"",
         "hfhfhf":"h",
         "hhfh":"hfjfhdj",
         "cat":"B"
      }
]

code i am using to add the new key value pair :
for (let i = 0; i < object.length; i++) {
            object[i]['ABC'] = [i];
        }

Actually expected should be:

var object =[
      {
         "asdf":"",
         "fdrtf":"869966",
         "hdhfhfh":"utytut",
         "Cat":"A",
         "ABC": "0"
         
      },
      {
         "hjhj":"",
         "hfhfhf":"h",
         "hhfh":"hfjfhdj",
         "cat":"B",
         "ABC": "1"
         
      },
      {
         "hjhj":"",
         "hfhfhf":"h",
         "hhfh":"hfjfhdj",
         "cat":"B"
         "ABC": "2"
      }
]

the final value i am getting as below:

["0":{
         "hjhj":"",
         "hfhfhf":"h",
         "hhfh":"hfjfhdj",
         "cat":"B",
      
   },
   "1":{
         "asdf":"",
         "fdrtf":"869966",
         "hdhfhfh":"utytut",
         "Cat":"A"
         
   },
   "2":{
       "hjhj":"",
         "hfhfhf":"h",
         "hhfh":"hfjfhdj",
         "cat":"B"
   }
  ]

kindly someone help me out to achieve this

3
  • Please do not repost questions or post under multiple accounts. If you need to make changes, you can edit a post.
    – David Reed
    Oct 29, 2020 at 15:02
  • Additionally, because this question has no specificity to Salesforce, Stack Overflow or introductory JavaScript resources such as the Mozilla Developer Network are likely to be the best help.
    – David Reed
    Oct 29, 2020 at 15:03
  • I am trying this in lwc of salesforcr
    – Rukmina
    Oct 29, 2020 at 15:11

1 Answer 1

2

The = [i] is what's causing the problem, at least in part. However, this isn't very JavaScript-y; you simply use Array.prototype.map instead:

object = object.map((value,index) => ({ ...value, ABC: index }));

Here we use Array.prototype.map, which uses a callback function to determine what the new value should be based on the old value. (params) => ({...})) is an arrow function. {...value} copies the old values to a new object (shallow copy) so we can work on the data, and then we assign the new property as well (, ABC: index).

6
  • do i need to add inside the for loop ,,,sorry but didn't understand..
    – Rukmina
    Oct 29, 2020 at 14:13
  • i tried inside the for loop but still getting the same result
    – Rukmina
    Oct 29, 2020 at 14:18
  • @Rukmina No, the for loop is not necessary at all.
    – sfdcfox
    Oct 29, 2020 at 14:24
  • getting this error : [e.map is not a function] without loop
    – Rukmina
    Oct 29, 2020 at 14:28
  • @Rukmina No idea what you did wrong. This is need correct, you use the map function for this purpose.
    – sfdcfox
    Oct 29, 2020 at 15:22

Not the answer you're looking for? Browse other questions tagged .