Home > Mobile >  I have objects in an array and I don't know how to print them
I have objects in an array and I don't know how to print them

Time:04-05

There are objects in the array and I don't know how to output them.

Without using functionalList[0][1][2]`, I want to print all the values ​​of id and content, but I don't know how to do it.

Printing hi will result in an error.

[
  {
    functionalList: [
      {
        id: 129,
        content: 123,
      },
    ],
  },
  {
    functionalList: [
      {
        id: 130,
        content: 35,
      },
    ],
  },
  {
    functionalList: [
      {
        id: 131,
        content: 32,
      },
    ],
  },
];

let hi = [
  values.functionalList.map((value: any) => {
    return {
      id: value.id,
      content: value.content,
    };
  }),
];

console.log(hi);

CodePudding user response:

I leave you an idea, put your array in a variable for example called 'data'

// Online Javascript Editor for free
// Write, Edit and Run your Javascript code using JS Online Compiler

var data = [
  {
    "functionalList": [
      {
        "id": 129,
        "content": 123
      }
    ]
  },
  {
    "functionalList": [
      {
        "id": 130,
        "content": 35
      }
    ]
  },
  {
    "functionalList": [
      {
        "id": 131,
        "content": 32
      }
    ]
  }
]


data.forEach(function(id){
  // Get the data of particular id. You can loop over the specific id to access it's properties
  Object.keys(id).forEach(function(prop){
    console.log(id[prop]);
  });
});

Adapt it to your needs

CodePudding user response:

To start you must remove the comma after "content": 35 and "content": 32 since they produce an error.

CodePudding user response:

You didn't specify what the error is (and you really should) but there are several clear problems with the code you've posted.

  1. You need to make sure your array is assigned to a variable as in your original code it's just a naked array expression.

  2. Unless you're writing TypeScript you need to remove the :any from your arrow function declaration as that is a TypeScript type declaration but a syntax error in standard JavaScript.

  3. You seem to have got the way you structure your map the wrong way round. The map iterates through all of the elements of the array and all of them are objects with a property of functionalList but you have called for that property on your array instead when you should be doing it inside the map function.

  4. Even then the functionalList sub-objects are themselves arrays with one item so you should be referencing what index you need. As there is only one index in the example I assume that's the one you want so you add [0] to the end.

So your map function should be:

values.map((value) => {
        return {
            id: value.functionalList[0].id,
            content: value.functionalList[0].content,
        }
    })

Putting it all together then this is the result.

let values = [
            {
                "functionalList": [
                    {
                        "id": 129,
                        "content": 123
                    }
                ]
            },
            {
                "functionalList": [
                    {
                        "id": 130,
                        "content": 35
                    }
                ]
            },
            {
                "functionalList": [
                    {
                        "id": 131,
                        "content": 32
                    }
                ]
            }

        ]

let hi = [
    values.map((value) => {
        return {
            id: value.functionalList[0].id,
            content: value.functionalList[0].content,
        }
    })
]
console.log(hi);

CodePudding user response:

if the value of functionalList will contain one data. you can use the method bellow :

const values = [
  {
    functionalList: [
      {
        id: 129,
        content: 123,
      },
    ],
  },
  {
    functionalList: [
      {
        id: 130,
        content: 35,
      },
    ],
  },
  {
    functionalList: [
      {
        id: 131,
        content: 32,
      },
    ],
  },
];

let hi = values.map((value) => {
  return {
    id: value.functionalList[0].id,
    content: value.functionalList[0].content,
  }
});

// adjust to what you want to do
hi.map((value) => {
  console.log(`Id: ${value.id}, Content: ${value.content}`);
});

  • Related