1

Hi i have a set of words that's retrieved by php and displayed on two hidden text field like this:

The titles:

Red Apple, Grapes, Green Apple, Orange

The file names:

red_apple5, grapes1, green_apple2, orange3

is there a way that i can convert this to an object or array something like this:

T = {"Red Apple" , "Grapes" , "Green Apple" , "Orange"}

S = {"red_apple5" , "grapes1" , "green_apple2" , "orange3"}

i want append each of them to a list. is there a way to do this?

2
  • 1
    {"Red Apple" , "Grapes" , "Green Apple" , "Orange"} - it is syntactically incorrect Commented Jul 5, 2012 at 22:55
  • Correct, curly braces ("{" or "}") notate an Object, which is a set of key: value pairs. EX: {name: "John", city: "compton"}. Square brackets notate arrays. EX ["one", "two", "three"]. Good reference found here: godbit.com/article/js-tidbits-datatypes-object-object-vs-array Commented Jul 5, 2012 at 23:07

4 Answers 4

2
var array = 'Red Apple, Grapes, Green Apple, Orange'.split(', ');
Sign up to request clarification or add additional context in comments.

2 Comments

What about S (the other object file names)
@Derek: I don't get you. I'm sure OP could understand that he may apply .split() to any string so I don't need to give example for every single sting in the world
2

You can use javascript's split() method to split the string:

var str = "Red Apple, Grapes, Green Apple, Orange";
var arr = str.split(', ');
console.log(arr)

To append these to a list, you must loop through the newly created array using forEach() and then create a new li for each item. Then you can append the li as a child element to a ul.

var list = document.getElementById("#IdOfYourList")
arr.forEach(function(a) {
    var newLi = document.createElement("li")
    newLi.innerHTML = a;
    list.appendChild(newLi);
});

Also note that forEach is not supported in IE < 8, but can be implemented into the Array prototype here: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/forEach

Reference: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/split

Comments

1

Assuming you want to create a valid object, utilize the following code for reference. NOTE: Some modifications were made for illustrative purposes.

HTML:

<div id="output"></div>

jQuery:

var titles = 'Red Apple, Grapes, Green Apple, Orange'.split(', ');
var fileNames = 'red_apple5, grapes1, green_apple2, orange3'.split(', ');

var newObject = {};

for(var i=0; i < titles.length; i++){
    newObject [titles[i]] = fileNames[i];
}

$('#output').text(JSON.stringify(newObject));

Demo: http://jsfiddle.net/GHgsT/

The code above loops through the titles array and then maps the titles[index] value as the object's property (key) and the corresponding fileNames[index] value as the property's value.

1 Comment

It is always better to beautify the code with JSON.stringify jsfiddle.net/DerekL/vjTFk/1
1

Use .split().

var str = "Red Apple, Grapes, Green Apple, Orange";
var array = str.split(", ");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.