-1

I want to change values of array of objects. The keys 1,2 are dynamic and come from json.

var rows = [
  {
    "1": "14",
    "2": "55",
    "named": "name1",
    "row": 7
  },
  {
    "1": "15",
    "2": "56",
    "named": "nameper",
    "row": 9
  },
  ...
]

to. The name having nameper should have per cent values.

[
    {
      "1": "14",
      "2": "55",
      "named": "name1",
      "row": 7
    },
    {
      "1": "15%",
      "2": "56%",
      "named": "nameper",
      "row": 9
    },
    ...
  ]
0

3 Answers 3

0
rows.map((singleItem) => {
   if(singleItem.named === "nameper"){
      singleItem["1"] = singleItem["1"] + "%";
      singleItem["2"] = singleItem["2"] + "%";
   }
})
Sign up to request clarification or add additional context in comments.

1 Comment

Arun V was a little faster than you, making your answer a duplicate
0

You can use forEach or map or a for-loop

Using forEach.

rows.forEach((row) => {
    if (row["named"] === "nameper") {
        row["1"] = row["2"] + "%";
        row["2"] = row["2"] + "%";
    }
})

You can use map in a similar way!

Using for loop

for (var row of rows) {
    if (row["named"] === "nameper") {
        row["1"] = row["2"] + "%";
        row["2"] = row["2"] + "%";
    }
}

Comments

0
rows.map(row => {
    if(row["named"] === "nameper") {
          row["1"] = `${row["1"]}%`
          row["2"] = `${row["2"]}%`
    }
})

you can use a map and check for the named and update it accordingly

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.