Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6291

How can I join a duplicate value within one object?

$
0
0

This is an example of the object:

[{index:  0,name: "name 1",key: "key 1",value: "value 1"},{index:  0,name: "name 1",key: "key 2",value: "value 2"}]

I want to make it so the duplicated value are maintained, while the different value are added to the corresponding keys like so:

[{index:  0,name: "name 1",key: {"key 1", "key 2"}value: {"value 1", "value 2"}}]

I've tried doing something like this:

const object : [{index:  0, name: "name 1", key: "key 1", value: "value 1"},                {index:  0, name: "name 1", key: "key 2", value: "value 2"}]let new = []    for (let i = 0 ; i < object.length ; i++) {      if (new.length === 0) {         new.push({          index: object[i].index,          name: object[i].name,          key: object[i].key,          value: object[i].value        })      } else if (new.length > 0) {        for (let a = 0 ; a < new.length ; a++) {          if (new[a].index === object[i].index &&              new[a].name === object[i].name &&              new[a].key !== object[i].key) {                new[a].key = object[i].key                new[a].value = object[i].value          }        }      }    }

But it ended up overwriting the previous values


Viewing all articles
Browse latest Browse all 6291

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>