简体   繁体   English

我试图写一个从数组创建一个简单的对象。 在我的数组中,我存储了变量:

[英]I am trying to write a create a simple object from an array. In my array, I have variable stored:

Is there any way to assign variable names as keys in an object? 有什么方法可以将变量名分配为对象中的键? For example, I have these variables which are stored in the array "stats" 例如,我有这些变量,它们存储在数组“ stats”中

var name = "Sally"
var age = 35
var city = "New York"

var stats = [name, age, city] 

And I want to create an object that uses the variable names as keys and variable values as the objects' values. 我想创建一个使用变量名称作为键,使用变量值作为对象值的对象。

example: 例:

var obj = {"name": "Sally", "age": 35, "city": "New York"}

I am doing this by creating a function and running a for loop through the array. 我通过创建一个函数并在数组中运行一个for循环来做到这一点。 Right now, I have key assigned to the index, which I know is wrong, but I don't know how to make it be the variables' names. 现在,我为索引分配了键,我知道这是错误的,但是我不知道如何使它成为变量的名称。

function objCreator (array) {
    var obj = {}; 
    for (var i = 0; i < array.length; i++) {
        var key = i;
        var value = array[i];
        obj[key] = value; 
    }
    return obj; 
}

this is what the function returns: 函数返回的结果是:

=> { '0': Sally',
  '1': 35,
  '2': 'New York'
 }

Any suggestions? 有什么建议么?

You need to store the key-names somewhere 您需要将键名存储在某个地方

var keyNames = ["name", "age", "city"];
function objCreator (array) 
{
    var obj = {}; 
    for (var i = 0; i < array.length; i++) 
    {
      obj[keyNames[i]] = array[i]; //observe change in this line here
    }
    return obj; 
}

暂无
暂无

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

相关问题 我试图将一个对象添加到我存储在我的状态对象中的嵌套数组中,但似乎有问题 - I am trying to add an object to a nested array I have stored in my state object, but seem to have an issue with it 我正在尝试在空格键上为 keydown eventListener 触发事件,它将在数组中显示随机单词。 如果条件语句不起作用 - I am trying to have event trigger on spacebar for a keydown eventListener that will display random word in array. If conditional statement not working 我有数据存储在Perl数组中。 如何使用JSON将它们转换为JavaScript数组? - I have data stored in Perl array. How do I convert them into JavaScript array using JSON? 我正在尝试从对象数组中检索数字数组 - I am trying to retrieve an array of numbers from an array of object 我正在尝试从数组 object 的这个 for 循环中显示我的图像 - I am trying to get my image to display from this for loop from the array object 我正在尝试从数组内的对象中推送值 - I am trying to push values from an object inside an array 我正在尝试将对象中的一些值推送到 3 维数组中 - I am trying to push some values from an object into a 3 dimensional array Angular JS:将对象推入数组。 它正在创建一个对象,然后创建另一个数组。 我需要它来创建单独的对象。 这可能吗? - Angular JS: Pushing objects into an array. It is creating an object, then another array. I need it to create separate objects. Is this possible? 尝试创建一个从数组中提取URL的函数。 的JavaScript - Trying to create a function which extracts a URL from an array. JavaScript 我正在尝试将我的多个数组方法调用简化为 if 语句或更简单的基于条件的语句 - I am trying to simplify my multiple array method calls into an if statement or something much more simple based on a condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM