-4
{"1":"val1","2":"val2","3":"val3"}

i want it to converted like this:

{"Id":"1","value":"val1","Id":"2","value":"val2","Id":"3","value":"val3"}

little Help Please would be much appricated

learnLWC
  • 1
  • 2
  • 7
    That's not valid. An object cannot have multiple `value` properties. – Bergi May 29 '20 at 07:14
  • Do you have json text, or a js object? Anyway, I guess you're looking for [`Object.entries`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries) – Bergi May 29 '20 at 07:15
  • 2
    you can have this: `[{"Id":"1","value":"val1"},{"Id":"2","value":"val2"},{"Id":"3","value":"val3"}]` if that is of any help... – Argee May 29 '20 at 07:15
  • 1
    Maybe `[{"Id":"1","value":"val1"}, {"Id":"2", "value": "val2"}]`? Loop your array using key-value and push to new array objects – Justinas May 29 '20 at 07:16
  • Please learn [the difference between JSON and the Object Literal Notation](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation). Are you talking about JSON or an object? "JSON object" is not an actual thing. – str May 29 '20 at 07:19
  • What have you tried thus far? – cookie May 29 '20 at 07:21

8 Answers8

3

You can't use the same key name in one object.

instead you can do this.

const origin = {"1":"val1","2":"val2","3":"val3"}


const converted = Object.entries(origin).map( ([key,value]) => ({id: key, value }) );
console.log(converted);
kyun
  • 9,710
  • 9
  • 31
  • 66
0

What you have posted is invalid.

What you might want is:

const object = {"1":"val1","2":"val2","3":"val3"};

console.log(Object.entries(object));

// or

console.log(Object.keys(object).map(i => ({Id: i, value: object[i]})));
Chase
  • 3,028
  • 14
  • 15
0

You could use a loop over Object.entries.

E.g. something like:

const newObjArr = [];
for(let [key, value] of Object.entries(obj)){
  newObj.push({Id: key, value}); 
}

The above would return an array of objects, but I'm sure you can amend it to your particular use case.

Rob Bailey
  • 920
  • 1
  • 8
  • 23
0
const data = {"1":"val1","2":"val2","3":"val3"};
const result = Object.keys(data).map((key) => ({ id: key, value: data[key] }));

The result will be [{ id: "1", value: "val1" }, { id: "2", value: "val2" }, { id: "3", value: "val3" }]

TopWebGhost
  • 335
  • 1
  • 8
0

As pointed out this is invalis. If you want to convert it if would look like this:

[{"Id":"1","value":"val1"},{"Id":"2","value":"val2"},{"Id":"3","value":"val3"}]

You can make an function that converts this.

const object = {"1":"val1","2":"val2","3":"val3"};

console.log(Convert(object));

function Convert(obj){
  return Object.keys(obj).map(i => ({Id: i, value: obj[i]}));
}
Tim567
  • 775
  • 1
  • 5
  • 22
0

You cannot do this. Object is a unique key value pair.

{"Id":"1","value":"val1","Id":"2","value":"val2","Id":"3","value":"val3"}

Suppose you want to merge two object and What if both the object has same key, it simply merge the last objects value and have only one key value.

Ashutosh
  • 1,029
  • 10
  • 23
0

You can convert your large object to several small objects and store them in an array as this snippet shows. (It could be much shorter, but this verbose demo should be easier to understand.)

// Defines a single object with several properties
const originalObject = { "1" : "val1", "2" : "val2", "3" : "val3" }

// Defines an empty array where we can add small objects
const destinationArray = [];

// Object.entries gives us an array of "entries", which are length-2 arrays
const entries = Object.entries(originalObject);

// `for...of` loops through an array
for(let currentEntry of entries){

  // Each "entry" is an array with two elements
  const theKey = currentEntry[0]; // First element is the key
  const theValue = currentEntry[1]; // Second element is the value

  // Uses the two elements as values in a new object
  const smallObject = { id: theKey, value: theValue };

  // Adds the new object to our array
  destinationArray.push(smallObject);

} // End of for loop (reiterates if there are more entries)

// Prints completed array of small objects to the browser console
console.log(destinationArray);
Cat
  • 4,141
  • 2
  • 10
  • 18
0
 const obj = {"1":"val1","2":"val2","3":"val3"}

  const newObject = Object.keys(obj).map(e => {
 return {ID: e , value : obj[e] }
   });

  console.log(newObject); // [ { ID: '1', value: 'val1' },
                                { ID: '2', value: 'val2' },
                              { ID: '3', value: 'val3' } ]

it will give u an array of object, later u need to convert it to object and flat the object:

How do I convert array of Objects into one Object in JavaScript?

how to convert this nested object into a flat object?

Vahid
  • 1,258
  • 1
  • 14
  • 14