在node.js中迭代JSON.array

时间:2015-07-06 19:59:24

标签: javascript php json node.js

我有一个JSONArray

[
    {
        "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"
    }
]
PHP中的

我使用:

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

迭代表名。

如何在node.js中执行此操作? (我发现了许多问题,但没有真正的答案。大多数都来自2011年)

4 个答案:

答案 0 :(得分:9)

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););
});

答案 1 :(得分:3)

您需要先将其反序列化为对象。

var arr = JSON.parse(<your json array>);
for(var i = 0; i < arr.length; i++)
{
  var tablename = arr[i].tablename;
}

答案 2 :(得分:0)

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.map(({name})=> console.log(name)) 

在js中进行迭代,用于...,map,forEach,reduce

答案 3 :(得分:0)

另一种遍历节点中数组的方法:

.jsx