最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - converting string array into a key value pair object - Stack Overflow

programmeradmin6浏览0评论

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

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

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

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

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

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

Share Improve this question edited Dec 10, 2014 at 22:46 Barmar 781k56 gold badges545 silver badges659 bronze badges asked Dec 10, 2014 at 22:44 soumsoum 1,1593 gold badges21 silver badges49 bronze badges 3
  • Where are you getting the output from? It looks like it is supposed to be JSON but it is not formatted properly. – LavaSlider Commented Dec 10, 2014 at 22:46
  • 1 Use split(':') to split the value into a property name and value. – Barmar Commented Dec 10, 2014 at 22:47
  • well I am getting the output from our friend google spreadsheets.google.com/feeds/list/… – soum Commented Dec 10, 2014 at 22:49
Add a comment  | 

5 Answers 5

Reset to default 15

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();
}

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]
    }

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)
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
  • http://www.ecma-international.org/ecma-262/6.0/#sec-object-initializer

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.

$.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.

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

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

发布评论

评论列表(0)

  1. 暂无评论