-1

In the code section is a typical JSON object I am getting back

Was wondering what is the best way to loop in NodeJS, lint for some reason doesn’t like me using for (let item of myArray) it complains about 'ForOfStatement' is not allowed.

My current output comes out as the following:

Cont { id: 'Something1', mycontent: { foo: '1', bar: '1' } }
Cont { id: 'Something2', mycontent: { foo: '3', bar: '7' } }

so losing some of my values which I am assuming is due to me using Object.Entries as its key value pairs, whats the best way to loop round and retain these keys?

myObject = [{
     "id": "Something1",
     "mycontent": [{
         "foo": "12",
         "foo": "1",
         "bar": "1"
     }]
 },
     {
         "id": "Something2",
         "mycontent": [{
             "foo": "3",
             "bar": "5",
             "bar": "7"
         }]
     }
]

Object.entries(myObject).forEach((item) => {
 let myContent = item[1];
 console.log("Cont", myContent);
});

I expect the output of my object appear as:

Something 1: foo: 12, 1 : bar 1; Something2 : foo: 3, bar: 5, 7

As I have duplicated keys weren't sure how to output these

sam
  • 109
  • 2
  • 3
  • 12
  • `myObject` is an **array** of objects. – PM 77-1 Aug 14 '19 at 18:13
  • Possible duplicate of [For-each over an array in JavaScript?](https://stackoverflow.com/questions/9329446/for-each-over-an-array-in-javascript) – PM 77-1 Aug 14 '19 at 18:17
  • looks like you are correct, myObject is an array of Objects. Not in front of my dev box so will copy the actual JSON later. – sam Aug 14 '19 at 18:58
  • Keys in JS objects must be unique. Check: https://stackoverflow.com/questions/12561482/javascript-how-to-iterate-object-with-two-the-same-keys-and-get-two-values – Kishan Aug 14 '19 at 20:54

4 Answers4

1

so losing some of my values which I am assuming is due to me using Object.Entries as its key value pairs

You are not losing the values because of Object.Entries, you lose them the moment you try to define your "myObject" object. In a JS object, each key is unique. When you try to define this object:

{
     "id": "Something2",
     "mycontent": {
         "foo": "3",
         "bar": "5",
         "bar": "7"
     }
 }

you basically set the "bar" key only once, with the second assignment overriding the first, and this is the reason you get for an output:

Cont { id: 'Something2', mycontent: { foo: '3', bar: '7' } }

instead of

Cont { id: 'Something2', mycontent: { foo: '3', bar: '5', bar: '7' } }

As a small test, try to run this code:

const myObj = {
    foo: '3',
    foo: '7'
};

console.log(myObj);

You'll notice that the output is {foo: "7"}, as the new value is not appended to the previous value, nor the same key added.

Saarett
  • 61
  • 1
  • 4
0

As far as I know, the key in any object gets stored as a string, and when you use the same key foo twice it would overwrite the first foo key value and the same with using bar twice, only the last bar key would be stored. You can manipulate the data being stored by converting foo and bar as an array to save multiple values.

  • Yes the JSON data that you have mentioned is invalid as the same key cannot be used to store 2 values. The previous value would be overwritten. You could append to the same key. – Apoorv Aug 14 '19 at 18:24
0

You need to loop over it with a method from the Array.prototype: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

myObject.forEach((item) => {
  const myContent = item[1];
  console.log("Cont", myContent);
});
TbWill4321
  • 8,626
  • 3
  • 27
  • 25
  • '''myObject.forEach((item) => { console.log("Content", item); });''' gives the same output – sam Aug 14 '19 at 18:26
  • Looks like I a forEach was what I wanted My object was the following and as a result of the same key was causing me issues, so would have been better for me to rephrase the question about how to obtain these values ```var MyArray = [ {"key":"key1", "value":"value1"}, {"key":"key2", "value":"value2"}, {"key":"key2", "value":"value3"} ]; ``` I managed to work it out, to pull my duplicated keys ``` myobject.map((c) => { const foo = c.mycontent .filter(label => label.type ==='foo') .map(label => label.code .join(', '); const bar = c.mycontent .filter( -filter on bar ``` – sam Aug 15 '19 at 19:39
0

I managed to work it out, to pull my duplicated keys

myobject.map((c) => { 
const foo = c.mycontent 
.filter(label => label.type ==='foo') 
.map(label => label.code 
.join(', '); 

const bar = c.mycontent 
.filter(  ....... filter on bar just like above 
sam
  • 109
  • 2
  • 3
  • 12