简体   繁体   English

将键推到数组或javascript对象?

[英]Pushing keys to an array or javascript object?

I need to create an array or js object with specific keys, which should be pushed to an empty array or js object. 我需要使用特定键创建一个数组或js对象,应将其推入一个空数组或js对象。 After that, I need to push some specific values according to that keys, something like this: 之后,我需要根据该键推送一些特定的值,如下所示:

var arr = ["john":["jval1","jval2"], "matt":["mval1","mval2","mval3"]]

I know that's not the rigt notation for a js object or an array, but that's the idea. 我知道这不是js对象或数组的绑定符号,但这就是想法。

This is basically what I'm trying to do: 这基本上就是我想要做的:

var keys = ["john","matt"];             //my keys
var jvals = ["jval1","jval2"];          //my values for key john
var mvals = ["mval1","mval2","mval3"];  //my values for key matt

var arr = [];

for(var i in keys) arr.push(keys[i])  //pushing the names (I need to push this names as keys so next lines should work)

for(var i in jvals) arr["john"].push(jvals[i])  //Trying to push john values, which doesn't work because "john is not a key"
for(var i in mvals) arr["matt"].push(mvals[i])  //Same.

This doesn't work at all, so how should I do this? 这根本不起作用,那么我应该怎么做呢?

Here is the more shorter version of the code, using forEach 这是使用forEach的代码的较短版本

 var keys = ["john","matt"]; //my keys var jvals = ["jval1","jval2"]; //my values for key john var mvals = ["mval1","mval2","mval3"]; //my values for key matt var object = {} keys.forEach(function(key, index) { object[key] = index == 0 ? jvals.slice() : mvals.slice() }); console.log(object) 

Is this you wanted? 这是你想要的吗?

Declare arr as object and add keys with empty array.and then push to your keys. 将arr声明为对象,并添加具有空array。的键,然后按到您的键。

 var keys = ["john","matt"]; //my keys var jvals = ["jval1","jval2"]; //my values for key john var mvals = ["mval1","mval2","mval3"]; //my values for key matt var arr = {}; for(var i in keys) arr[keys[i]]=[]; for(var i in jvals) arr["john"].push(jvals[i]); for(var i in mvals) arr["matt"].push(mvals[i]); console.log(arr); 

var keys = ["john","matt"];             //my keys
var jvals = ["jval1","jval2"];          //my values for key john
var mvals = ["mval1","mval2","mval3"];  //my values for key matt

var arr = {}; // not []

for(var i in keys) arr.[keys[i]] = [];  //pushing the names (I need to push this names as keys so next lines should work)

for(var i in jvals) arr["john"].push(jvals[i])  //Trying to push john values, which doesn't work because "john is not a key"
for(var i in mvals) arr["matt"].push(mvals[i])  //Same.

OK, I see what you are trying to do. 好的,我知道您正在尝试做什么。 The first issue with your code is what you do to the arr array after pushing the keys 's values into it: 代码的第一个问题是将keys的值推入arr数组后的处理方式:

for(...) {
    arr["john"].push(jvals[i]);
}

What you are doing is pretty much saying arr.john.push(...) , but arr doesn't have a property named john that is array. 您正在做的几乎是说arr.john.push(...) ,但是arr没有名为john的属性,它是array。 What arr does have is an element at the index 0 that has the value of "john" . arr确实有一个索引为0的元素,其值为"john" Ie arr pretty much looks like this arr = ["john", "matt"] . arr几乎像这样arr = ["john", "matt"]

Now what you want is to do is change arr to be an object and not an array. 现在,您要做的就是将arr更改为对象而不是数组。

This way your code will look like so: 这样,您的代码将如下所示:

var keys = ["john","matt"];
var jvals = ["jval1","jval2"];
var mvals = ["mval1","mval2","mval3"];

var arr = {};

arr["john"] = jvals;
arr["matt"] = mvals;

// Please don't do the above

// Doing the following (using an object literal) is much better, and what most will advice when creating objects in js.

var arr = {
    john: ["jval1","jval2"],
    matt: ["mval1","mval2","mval3"]
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM