Javascript 将多维数组中的子数组转换为字符串
我正在尝试更新下面提到的多维JSON文件中的单个值(ElementStatus)Javascript 将多维数组中的子数组转换为字符串,javascript,arrays,json,react-native,multidimensional-array,Javascript,Arrays,Json,React Native,Multidimensional Array,我正在尝试更新下面提到的多维JSON文件中的单个值(ElementStatus) BuildingID: 1521 BuildingName: "PEN LLOYD BUILDING" BuildingNumber: "A" ElementList: Array(15) 0: {ElementID: 114, SurveyTypeID: 3, Code: "M.01.01.01", ElementDescription: "M.01.01.01 Boilers/Plant", ElementSt
BuildingID: 1521
BuildingName: "PEN LLOYD BUILDING"
BuildingNumber: "A"
ElementList: Array(15)
0: {ElementID: 114, SurveyTypeID: 3, Code: "M.01.01.01", ElementDescription: "M.01.01.01 Boilers/Plant", ElementStatus: "null"}
1: {ElementID: 115, SurveyTypeID: 3, Code: "M.01.01.02", ElementDescription: "M.01.01.02 Heat Emitters", ElementStatus: "null"}
2: {ElementID: 116, SurveyTypeID: 3, Code: "M.01.01.03", ElementDescription: "M.01.01.03 Distribution", ElementStatus: "completed"}
这是代码
var newData=JSON.parse(success);
const data1 = newData[0].results.recordset[0].ElementList;
//console.log(data1.toArray());
var array=JSON.parse(data1)
array.forEach(function(element){
if(element.ElementDescription==elementsName)
{
element.ElementStatus="completed"
}
})
newData[0].results.recordset[0].ElementList=array
在遍历forEach循环之后,我得到数组格式的ElementList。
但是我希望它是字符串格式的,这就是它以前的格式。您显示的代码有很多问题。您显示的数据不是正确的JSON格式,因此会立即失败 我修改了您的示例,用JSON输出显示正确的输入
let elementsName = "M.01.01.01 Boilers/Plant";
let success = `{"BuildingID": 1521,
"BuildingName": "PEN LLOYD BUILDING",
"BuildingNumber": "A",
"ElementList":
[{"ElementID": 114, "SurveyTypeID": 3, "Code": "M.01.01.01", "ElementDescription": "M.01.01.01 Boilers/Plant", "ElementStatus": null},
{"ElementID": 115, "SurveyTypeID": 3, "Code": "M.01.01.02", "ElementDescription": "M.01.01.02 Heat Emitters", "ElementStatus": null},
{"ElementID": 116, "SurveyTypeID": 3, "Code": "M.01.01.03", "ElementDescription": "M.01.01.03 Distribution", "ElementStatus": "completed"}]}`;
let newData=JSON.parse(success);
newData.ElementList.forEach(function(element){
if(element.ElementDescription==elementsName)
{
element.ElementStatus="completed"
}
});
let outputString = JSON.stringify(newData);
console.log(outputString);
所以您将其解析为数组。。。。。用stringify撤销它。似乎是一个非常明显的问题eNewData.ElementList.forEach(函数(元素){if(元素.ElementDescription==elementsName){element.ElementStatus=“completed”});无法读取undefinedCheck的属性“forEach”,请检查ElementList是否为正确的名称,并且它实际上是一个数组。下面是演示代码: