本文介绍了将对象推入数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这很简单,但我无法理解.

I know it's simple, but I cant get it.

我有此代码:

var nietos = [];
nietos.push(nieto.label);
nietos.push(nieto.value);

label是一些字符串,例如"Title"和值"Ramones".如果这样做,我将得到一个简单的数组,如

label is some string like "Title" and value "Ramones". If I do this I'll get a simple array like

["Title", "Ramones"]

我需要创建这个:

[{"01":"Title", "02": "Ramones"}]

我该如何将它们推入nietos数组以将它们推为对象?数字01,02将使用一些i,k变量生成,因为所有这些都在for中.

How can I do those push to the nietos array in order to push them as objects? the numbers 01, 02 will be generated with some i,k vars because all this is inside a for.

推荐答案

您必须创建一个对象.将值分配到对象.然后将其推入数组:

You have to create an object. Assign the values to the object. Then push it into the array:

var nietos = [];
var obj = {};
obj["01"] = nieto.label;
obj["02"] = nieto.value;
nietos.push(obj);

这篇关于将对象推入数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 03:41