Javascript 将字符串数组转换为键值对对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27412260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:07:42  来源:igfitidea点击:

converting string array into a key value pair object

javascriptjquery

提问by soum

I am getting an output which looks like this

我得到一个看起来像这样的输出

var x = ["title: x_one", " description: 1", " value: 4"] 

where x[0]returns title: x_one

其中x[0]收益title: x_one

Which is a string. I cant read the property of title. How would I convert it into an object so that eventually I will be able to loop through the array and read the properties such as title, description and value.

这是一个字符串。我无法阅读标题的属性。我将如何将它转换为一个对象,以便最终我将能够遍历数组并读取诸如标题、描述和值之类的属性。

I am trying to do this through jquery

我正在尝试通过 jquery 做到这一点

I have been looking for a solution but havent really found one. If there is any out there which I am missing I would highly appreciate if anyone else have and could point me to that

我一直在寻找解决方案,但还没有真正找到。如果有任何我遗漏的地方,如果其他人有并可以指出我,我将不胜感激

回答by Barmar

Loop through your array, splitting the values at the :character. Then use the first part as a property name, the second part as the value, in an object.

循环遍历数组,拆分:字符处的值。然后在对象中使用第一部分作为属性名称,第二部分作为值。

var obj = {};
for (var i = 0; i < x.length; i++) {
    var split = x[i].split(':');
    obj[split[0].trim()] = split[1].trim();
}

回答by Yehia Awad

Try this function I have already tested it

试试这个功能我已经测试过了

 var a=new Array();
    for(var i=0;i<x.length;i++){
    var tmp=x[i].split(":")
    a[tmp[0]]=tmp[1]
    }

回答by David Wickstr?m

A more up to date version that that uses some newer language

使用一些较新语言的更新版本

const splitStr = (x) => {
  const y = x.split(':');
  return {[y[0].trim()]: y[1].trim()};      
}

const objects = ["title: x_one", " description: 1", " value: 4"].map(splitStr)

console.log(objects)

回答by Prashanth Thurairatnam

Just bumped into this. Here is another solution for OP's original array.

刚碰到这个。这是 OP 原始数组的另一种解决方案。

var x = ["title: x_one", " description: 1", " value: 4"] 


function mapper(str)
{
    var o = {};
    strArr = str.split(":");
    o[strArr[0].trim()] = strArr[1].trim();
    return o;
}


var resultArray = x.map(mapper);
console.log(resultArray[0].title);
console.log(resultArray[1].description);
console.log(resultArray[2].value);

fiddle

小提琴

回答by Baer

Assuming you have an object before you create this array you don't need to convert this to anything. Using jQuery's each you can get the value and the key.

假设您在创建此数组之前有一个对象,则无需将其转换为任何内容。使用 jQuery 的 each 你可以获得值和键。

$.each( obj, function( key, value ) {
  alert( key + ": " + value );
});

If this is the final output then you can just use String.prototype.split when you loop through.

如果这是最终输出,那么您可以在循环时使用 String.prototype.split。