简体   繁体   中英

How do I convert JSON values into a key value pair pir

I have a JSON in the following format and need to convert the 2 values into a Key / Value pair in javascript

"column_values": [ { "id": "status", "text": "Working on it" } ]

I need the result to be

"column_values"[{"status": "Working on it"}]

I need the code to iterate through the column_values array and convert all the sets of id and text pairs to the key = id value: Value = text:values

Is my result possible?

Additional Information...

I am parsing a response from monday.com api in zapier.

the api payload is contained in

const results = response.json;

the full api payload is

{
  "data": {
    "boards": [
      {
        "name": "Test Board",
        "items": [
          {
            "name": "Name Change",
            "id": "625495642",
            "column_values": [
              {
                "id": "person",
                "text": ""
              },
              {
                "id": "subitems",
                "text": "Subitem 1, Subitem 2"
              },
              {
                "id": "status",
                "text": "Working on it"
              },
              {
                "id": "dropdown",
                "text": "Test1"
              },
              {
                "id": "formula",
                "text": ""
              }
            ]
          }
        ]
      }
    ]
  },
  "account_id": 1111
}

I need to the the code to parse the data and replace the column_values with the format above, and then pass the reformated payload to

return results;

You just Map the Array you start out with to an Array with the values.

 var column_values = [ { "id": "status", "text": "Working on it" } ] var KeyValuePairs = column_values.map(cv => [cv.id,cv.text]); console.log(KeyValuePairs);

If every object is going to contain the id and text keys only, you can map it and delete the other keys.

column_values = column_values.map(item => {
  item[item.id] = item.text;
  delete item.id;
  delete item.text;
  return item;
});

try this

 var column_values = [ { "id": "status", "text": "Working on it" } ] var res = column_values.map(x => ({[x.id]: x.text})) console.log(res)

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