6

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

3
  • Where are you getting the output from? It looks like it is supposed to be JSON but it is not formatted properly. Commented Dec 10, 2014 at 22:46
  • 1
    Use split(':') to split the value into a property name and value. Commented Dec 10, 2014 at 22:47
  • well I am getting the output from our friend google spreadsheets.google.com/feeds/list/… Commented Dec 10, 2014 at 22:49

5 Answers 5

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();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks man..you have answered few of my questions in the past and those were right on :-). Thanks for all your help and helping understanding the concepts
2

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

1 Comment

Except for your inappropriate use of new Array(), that's identical to my answer.
2

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)

1 Comment

Good use of Lambda (MOZ - Array Functions) functionality. Although newbies & some mid-levels may mis-use without good knowledge and experience from other languages for its Pro's & Con's, even for JS/JQ
0

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.

1 Comment

It's an array, not an object. The keys will just be array indexes 0, 1, etc.
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.