Javascript 推入数组的第一个对象

Javascript 推入数组的第一个对象,javascript,arrays,Javascript,Arrays,我想将object的对象推入数组的第一个对象。 这是我的密码: const titles = { name: 'Not 1', name2: 'Not 2', }; const array = [{ type: 'type', titles: [] }]; let e; for (const name in titles) { e = titles[name]; } array.push({ name: e }); 这是我得到的结果: [ { "type": "ty

我想将object的对象推入数组的第一个对象。 这是我的密码:

const titles = {
  name: 'Not 1',
  name2: 'Not 2',
};

const array = [{ type: 'type', titles: [] }];
let e;
for (const name in titles) {
  e = titles[name];
}
array.push({ name: e });
这是我得到的结果:

[
  { 
    "type": "type", 
    "titles": [] 
  }, 
  { "name": "Not 2" }
]
这就是我想要的结果:

[
  { 
    "type": "type", 
    "titles": [
      { "name": "Not 1" },
      { "name": "Not 2" }
    ] 
  }
]

访问该对象并使用“推”按钮推送阵列中的其他对象

常量标题={ 名称:'不是1', 名称2:‘不是2’, }; 常量数组=[{ 类型:“类型”, 标题:[] }, { 类型:“类型”, 标题:[] }]; 让e; 用于标题中的常量名称{ e=标题[名称]; array.forEachx=>x.titles.push{ 姓名:e } }
console.logarray只需选择正确的数组元素并推送到它的内部数组

const titles = {
  name: 'Not 1',
  name2: 'Not 2',
};

const array = [{ type: 'type', titles: [] }];

Object.keys(titles).forEach((key) => {
    array[0].titles.push({name: titles[key]})

})
您可以将值推送到数组[0]。标题

常量标题={ 名称:'不是1', 名称2:‘不是2’, }; 常量数组=[{type:'type',titles:[]}]; 用于标题中的常量名称{ 数组[0]。标题。标题[名称] } console.logarray 您可以将Object.entires与Array.from一起使用,形成一个单独对象标题的数组,然后将标题属性设置为新数组:

常量标题={ 名称:'不是1', 名称2:‘不是2’, }; 常量数组=[{type:'type',titles:[]}]; 数组[0]。titles=array.fromObject.valuestitles,v=>{names:v};
console.logarray 以防您要推送到项目数组。使用map和Object.entries

常量标题={ 名称:'不是1', 名称2:‘不是2’, }; 常量数组=[{ 类型:“类型”, 标题:[] }, { 类型:“类型2”, 标题:[] }]; //在插入之前重新构建标题 const newTitles=Object.entriestiles.map[first,second]=>{[first]:second} //为项目数组插入 const rs=array.map{titles,…rest}=>{…rest,titles:newTitles}
这对我有用,谢谢。但是我想把名字放到数组标题中的不同对象谢谢你的帮助,我找到了一种方法