Javascript 如何在java脚本中创建对象数组
我想在JavaScript中定义对象的Javascript 如何在java脚本中创建对象数组,javascript,arrays,object,Javascript,Arrays,Object,我想在JavaScript中定义对象的数组。具有两个属性的对象第一个是文本,第二个是布尔值。首先它是空的,然后我在数组中逐个添加成员 myarray = [ {"text1",true}, {"text2",false}, {"text3",false} ]; 之前不需要定义结构,只需添加项即可 let myarray = []; console.log(myarray); // one by one myarray.push({ text: 'text1', boolean: true }
数组。具有两个属性的对象第一个是文本
,第二个是布尔值
。首先它是空的,然后我在数组中逐个添加成员
myarray = [
{"text1",true},
{"text2",false},
{"text3",false}
];
之前不需要定义结构,只需添加项即可
let myarray = [];
console.log(myarray);
// one by one
myarray.push({ text: 'text1', boolean: true });
console.log(myarray);
// more at once
myarray = myarray.concat(
[
{ text: 'text2', boolean: false },
{ text: 'text3', boolean: false }
]
);
console.log(myarray);
您可以创建数组并按如下方式定义其数据:
var myarray = [];
myarray.push({ text: 'text1', boolean: true });
myarray.push({ text: 'text2', boolean: false});
myarray.push({ text: 'text3', boolean: false});
var myArray=[text1,text2,text3];
var objArray=[];
for(让index=0;index
发布您的编码尝试。请访问,查看内容和。做一些研究,搜索相关话题等;如果你被卡住了,张贴一份你的尝试,记下输入和预期输出。
var myArray = [text1,text2,text3];
var objArray= [];
for (let index = 0; index < myArray.length; index++) {
var newItem = {};
newItem = {
name: myArray[index],
value: true
}
objArray.push(newItem);
}