Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在nodejs中将数据传递到JSON数组?_Javascript_Node.js_Arrays_Json_For Loop - Fatal编程技术网

Javascript 如何在nodejs中将数据传递到JSON数组?

Javascript 如何在nodejs中将数据传递到JSON数组?,javascript,node.js,arrays,json,for-loop,Javascript,Node.js,Arrays,Json,For Loop,我想将数据从一个JSON数组传递到另一个JSON数组。我的第一个json数组如下: var data = [{"Commodity":"Apparel and clothing accessories; knitted or crocheted"},{"Commodity":"Fish and crustaceans, molluscs and other aquatic invertebrates"},{"

我想将数据从一个JSON数组传递到另一个JSON数组。我的第一个json数组如下:

var data = [{"Commodity":"Apparel and clothing accessories; knitted or crocheted"},{"Commodity":"Fish and crustaceans, molluscs and other aquatic invertebrates"},{"Commodity":"Meat, fish or crustaceans, molluscs or other aquatic invertebrates; preparations thereof"},{"Commodity":"Natural, cultured pearls; precious, semi-precious stones; precious metals, metals clad with precious metal, and articles thereof; imitation jewellery; coin"},{"Commodity":"Sugars and sugar confectionery"}];
我想将每个商品值传递给我的第二个JSON数组。我尝试使用for循环:

var arr = [];
for(let j = 0; j < data.length; j++) {
    arr = [{"title":"Select a commodity:","options":[{"label":data[j]['Commodity'],"value":{"input":{"text":data[j]['Commodity']}}}],"description":"","response_type":"option"}];
}
我需要在label和text属性中填充每个商品值。然而,在产量方面,只有最后一种商品——糖和糖果在arr中展示

任何想法都会有帮助。多谢各位

更新:

这是输出的样子,但选择商品只应打印一次。这是ibm watson assistant的响应类型选项。

编辑:

let arr = {
title: "Select a commodity:",
response_type: "option",
description: "",
options:data.map(element => {
  return {
    label: element.Commodity,
    value: {text: element.Commodity}
  }
}) 
}
console.log(arr);
输出

{
response_type: "option"
title: "Select a commodity:"
description: ""
options: Array(5)
0: {label: "Apparel and clothing accessories; knitted or crocheted", value: {…}}
1: {label: "Fish and crustaceans, molluscs and other aquatic invertebrates", value: {…}}
2: {label: "Meat, fish or crustaceans, molluscs or other aquatic invertebrates; preparations thereof", value: {…}}
3: {label: "Natural, cultured pearls; precious, semi-precious …, and articles thereof; imitation jewellery; coin", value: {…}}
4: {label: "Sugars and sugar confectionery", value: {…}}
}
问题是您实际使用的是=运算符,它将值重新分配给数组-arr。 而不是arr=[{。。。 尝试使用:

var arr = [];
for(let j = 0; j < data.length; j++) {
  arr.push({"title":"Select a commodity:","options":[{"label":data[j]['Commodity'],"value":{"input":{"text":data[j]['Commodity']}}}],"description":"","response_type":"option"});
}

这些是数组,不是JSON数组。JSON是一种文本格式。与arr=[{…}]不同,arr=[{…}]每次都会覆盖数组,只需使用arr.push{…}@AntonMitsev i did因为您得到的答案代码完全符合您的要求,但显然您在寻找不同的东西,请在您的问题中添加所需输出的示例。@ChrisG i更新了问题中的输出。感谢您的回答,但是否有办法仅在标签和文本中添加商品值?以下是标题、描述和响应类型应保持唯一。@Rohan它们如何保持唯一?在for循环之前数组是空的。@Rohan我想这就是您要求的??让arr={标题:选择商品:,响应类型:选项,描述:,选项:data.mapelement=>{返回{label:element.Commodity,value:{text:element.Commodity}}}}console.logarr;@puneetsethei感谢您的帮助。