Home > Enterprise >  NodeJS: JSON Array - Parse/Decode from Array
NodeJS: JSON Array - Parse/Decode from Array

Time:12-01

I have a JSON Array with:

{
    "status": 200,
    "user": "niebauer",
    "channels": [
        {
            "name": "maxmustermann",
            "followers": 17193,
            "views": 650255,
            "partnered": false
        },
        {
            "name": "harrypotter",
            "followers": 21487,
            "views": 5110,
            "partnered": false
        },
        {
            "name": "welten",
            "followers": 1017,
            "views": 9318,
            "partnered": false
        },
        {
            "name": "meeresbuecher",
            "followers": 5141,
            "views": 61411,
            "partnered": false
        },
        {
            "name": "weltrekord",
            "followers": 171777,
            "views": 17832138,
            "partnered": true
        },
        {
            "name": "tvtotaler",
            "followers": 2117,
            "views": 21300,
            "partnered": false
        },
        {
            "name": "kramkiste",
            "followers": 6819,
            "views": 30414,
            "partnered": false
        }
    ],
    "cursor": ""
}

And now i want to have in my output only the name from the channels. It should be: maxmustermann harrypotter welten ...

How can I do this in Node.js? I found many questions with it, but no actual answer. Most of them are from 2012 and older. Thanks.

In this there are 7 channels. How can i get the counter aswell for it? So i get a output of "7"?

CodePudding user response:

use .map

console.log(data.channels.map((channel) => channel.name)) 
// ["maxmustermann" ,"harrypotter", "welte",...]

CodePudding user response:

Here you can try this logic and get counter also :

let obj = {
  status: 200,
  user: "niebauer",
  channels: [
    {
      name: "maxmustermann",
      followers: 17193,
      views: 650255,
      partnered: false,
    },
    {
      name: "harrypotter",
      followers: 21487,
      views: 5110,
      partnered: false,
    },
    {
      name: "welten",
      followers: 1017,
      views: 9318,
      partnered: false,
    },
    {
      name: "meeresbuecher",
      followers: 5141,
      views: 61411,
      partnered: false,
    },
    {
      name: "weltrekord",
      followers: 171777,
      views: 17832138,
      partnered: true,
    },
    {
      name: "tvtotaler",
      followers: 2117,
      views: 21300,
      partnered: false,
    },
    {
      name: "kramkiste",
      followers: 6819,
      views: 30414,
      partnered: false,
    },
  ],
  cursor: "",
};

let result = obj.channels.reduce((prev, curr, index) => {
    prev.name.push(curr.name);
    prev.count  ;
    return prev;
  },
  { name: [], count: 0 }
);

console.log(result);

CodePudding user response:

Simply do this with .map function:

const channelNames = data.channels.map((channel) => channel.name);
console.log(channelNames);

Or by looping through the items using .forEach method and then push each name into an array like this:

const channelNames = [];
data.channels.forEach((channel) => {
  channelNames.push(channel.name);
});
console.log(channelNames);
  • Related