create a summary object of an arrangement

0

I'm looking for a way to create an object that contains a summary with all the existing parameters in each item of the array. sample tel arrangement:

const peopleArray = [
  { id: 123, name: "dave", age: 23 , ar: [{id:1},2,3]},
  { id: 456, name: "chris", age: 23 },
  { id: 789, name: "bob", age: 23 },
  { id: 123, name: "dave", age: 23 , ar: [{ir:1},2,3]},

]

exit

     { "age": 23,
       "id": 102,
       "name":"chris" 
       "tim",
       "ar": [{
              "id": 1,
              "ir": 1
              },
              2,3
             ]
    }

The important thing for me is to be able to get all the keys at the same level that was found, use the function array.reduce, but I do not get all the parameters.

Why am I looking to do this? I have an arrangement with 100 items, each item has certain parameters that I want to add, so I want to make 1 object that has all the keys and the total of each one.

    
asked by Luis Ruiz Figueroa 17.10.2018 в 20:59
source

1 answer

0

I think you have not explained yourself very well. In the first code that looks like JS, as you have been told in comments, you do not have the JSON well formed, since all the keys of a JSON object must be in quotes.

In addition, each of the elements of the array (the JSON objects), should be enclosed in quotation marks, and the last element of the array is not followed by a comma. The JSON chain should look like this:

const peopleArray = [
  "{ 'id': 123, 'name': 'dave', 'age': 23 , 'ar': [{'id':1},2,3]}",
  "{ 'id': 456, 'name': 'chris', 'age': 23 }",
  "{ 'id': 789, 'name': 'bob', 'age': 23 }",
  "{ 'id': 123, 'name': 'dave', 'age': 23 , 'ar': [{'ir':1},2,3]}"
]

If you want to obtain the items from the array and pars them to objects, we would need to know which language you are going to parse with: PHP, Java, Javascript ...

What you seem to want is to obtain each of the items in the array and print them, no ?. It does not have much mystery of the first (although then I see that you mix objects from the array of the property 'ar' in the output, which does not make much sense). If you pair each of the Strings of the array to a JSON object and print it, you should have the result you are looking for. I leave a link where you can learn the basics of JSON in Javascript.

link

    
answered by 17.10.2018 в 21:51