0

In my application i need to get some data from JSON and convert it to an array for further use.

And I have my codes to get and convert the data:

var Persons = data.Persons;
var PersonsArr = [];

for (var i=0; i < Persons.length; i++){
    PersonsArr.push(Persons[i].word);
    }

This is quite simple, i just call the api and use a single for loop to get everything. The out come is like

PersonsArr=["peter","mary","tom"];

Then i append it to an element but it has "," to split each of the data and i'd like to remove it.

So i tried to use .replace()

for (var i=0; i < Persons.length; i++){
    PersonsArr.push(Persons[i].word.replace(",", " "));
                }

But it's not working as my exceptation, am i using the wrong apporch?

1
  • 1
    There are no commas. This is just JavaScript printing the array in a form you can read. But there are no commas internally in the array, so you are doing just fine Commented Mar 30, 2016 at 3:16

3 Answers 3

1

Then i append it to an element but it has ","

When an array is appended as .innerHTML or .textContent to an html element the array is converted to a string, for example, compare results of PersonsArr.toString() .


Use Array.prototype.join()

Syntax
str = arr.join([separator = ','])

Parameters

separator
Optional. Specifies a string to separate each element of the array. The separator is converted to a string if necessary. If omitted, the array elements are separated with a comma. If separator is an empty string, all elements are joined without any characters in between them.

with " " space character as parameter when appending PersonArr to element as html

PersonArr.join(" ") 

var PersonsArr = ["peter", "mary", "tom"];
var div = document.querySelector("div");
var text = document.createElement("text");
text.textContent = PersonsArr.join(" ");
div.appendChild(text);
<div></div>

Sign up to request clarification or add additional context in comments.

Comments

0

don't know about the rest of it, but this could cause the error (the g stands for a global change throughout the string)

.replace(",", " "))

should be

.replace(/,/g, " "))

Comments

0

Well, if you're trying to remove ',' from PersonsArr=["peter","mary","tom"]; you can't. Because that's an array and thats how it stores the value. And its not added by the data you get from the api. So if you try to access personArr[0] it'll return peter and there'll be no ','. If this is not what you're trying to do please let me know.

2 Comments

should i use .slice()
There is no ',' there actually. But still if you don't want to see it then don't put it inside an array. `var personArr = ""; for (var i=0; i < Persons.length; i++){ PersonsArr+=Persons[i].word; } In this case there'll be no ',' or' " '

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.