Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate over a JSON array in Node.js?

I have a JSON array:

[
    {
        "art": "A",
        "count": "0",
        "name": "name1",
        "ean": "802.0079.127",
        "marker": "null",
        "stammkost": "A",
        "tablename": "IWEO_IWBB_01062015"
    },
    {
        "art": "A",
        "count": "0",
        "name": "2",
        "ean": "657.7406.559",
        "marker": "null",
        "stammkost": "A",
        "tablename": "IWEO_IWBB_02062015"
    }
]

To iterate over the array in PHP I would use the following code to iterate over the tablenames:

foreach($jArray as $value){ 
  $tablename = $value['tablename'];
  //some code
}

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

like image 789
Piet Avatar asked Jul 06 '15 19:07

Piet


People also ask

How do I iterate a JSON array in node JS?

To iterate JSON array, use the JSON. parse().

How do I iterate through a JSON object in HTML?

Use Object.values() or Object. entries(). These will return an array which we can then iterate over. Note that the const [key, value] = entry; syntax is an example of array destructuring that was introduced to the language in ES2015.

How do you iterate through a JSON object in react?

The map() method is the most commonly used function to iterate over an array of data in JSX. You can attach the map() method to the array and pass a callback function that gets called for each iteration. When rendering the User component, pass a unique value to the key prop.


1 Answers

var tables = [
    { "art":"A","count":"0","name":"name1","ean":"802.0079.127","marker":"null","stammkost":"A","tablename":"IWEO_IWBB_01062015" },
    { "art":"A","count":"0","name":"2","ean":"657.7406.559","marker":"null","stammkost":"A","tablename":"IWEO_IWBB_02062015" }
];

tables.forEach(function(table) {
    var tableName = table.name;
    console.log(tableName);
});
like image 82
Clarkie Avatar answered Sep 19 '22 02:09

Clarkie