Lance
Lance

Reputation: 53

Convert json to array

I am trying to convert a json string to an array but i still get json code in return when i parse it.

var slag = [];
                for(var i=0; i < data.length; i++)
                {
                    var string = JSON.stringify(data[i]);
                    var parse = JSON.parse(string)
                    slag.push(parse);
                }
                console.log(slag);

now i still get this output

0: {id: "4", club: "driver", afstand: "230", shot: "straight", uitvoering: "perfect"}
1: {id: "9", club: "ijzer7", afstand: "140", shot: "straight", uitvoering: "perfect"}

but i want something like this in an array

[4, "driver", 240, "straight", "perfect"]

Upvotes: 4

Views: 23331

Answers (4)

Neel Rathod
Neel Rathod

Reputation: 2111

I can't figure out your expected outcome, But I have 2 solutions for you If you want your response like:

[
  "4",
  "driver",
  "230",
  "straight",
  "perfect",
  "9",
  "ijzer7",
  "140",
  "straight",
  "perfect"
]

Then you need to take values of an object Object.values(element) and make another loop over it and push it in slug

const array = [{
    id: "4",
    club: "driver",
    afstand: "230",
    shot: "straight",
    uitvoering: "perfect"
}, {
    id: "9",
    club: "ijzer7",
    afstand: "140",
    shot: "straight",
    uitvoering: "perfect"
}];

const slug = [];

 for (let i= 0; i < array.length; i += 1) {
    const element = array[i];
    Object.values(element).forEach((r) => { slug.push(r) });
 }

console.log(slug);

If you want your outcome like this:

[
  [
    "4",
    "driver",
    "230",
    "straight",
    "perfect"
  ],
  [
    "9",
    "ijzer7",
    "140",
    "straight",
    "perfect"
  ]
]

Then you just need to add map() method only

const array1 = [{
    id: "4",
    club: "driver",
    afstand: "230",
    shot: "straight",
    uitvoering: "perfect"
}, {
    id: "9",
    club: "ijzer7",
    afstand: "140",
    shot: "straight",
    uitvoering: "perfect"
}];

const slug = array1.map((m) => { return Object.values(m); });
console.log(slug);

In both cases Object.values() is used for such operations

Upvotes: 2

Dan Walsh
Dan Walsh

Reputation: 127

I'm a little confused as to why you're stringifying it and then parsing it right after? date[i] and parse would have the same output unless I'm missing something.

If you want to deconstruct an objects values into an array you can use the Object.values method.

Consider the following:

let x = {id: "4", club: "driver", afstand: "230", shot: "straight", uitvoering: "perfect"};

console.log(Object.values(x)); 
// output: ["4", "driver", "230", "straight", "perfect"]

So if you wanted a multi-dimensional array (not sure why this would be desired but it looks like that's what you're trying to accomplish) you could do this:

var slag = [];

for(var i=0; i < data.length; i++) {
    slag.push(Object.values(data[i]);
}

console.log(slag);

That would output:

0: ["4", "driver", "230", "straight", "perfect"]
1: ["9", "ijzer7", "140", "straight", "perfect"]

Upvotes: 0

Pranav C Balan
Pranav C Balan

Reputation: 115262

You need to extract values from the object, use Object.values method after parsing the JSON string.

var parse = Object.values(JSON.parse(string))

FYI : You can do it directly to the object instead of stringifying and parsing(which doesn't makes any sense). So simply iterate and extract values from object using Object.values method.

for(var i=0; i < data.length; i++){
    slag.push(Object.values(data[i]));
}

Upvotes: 7

Mohammed Amir Ansari
Mohammed Amir Ansari

Reputation: 2401

Simply do

let data = [{id: "4", club: "driver", afstand: "230", shot: "straight", uitvoering: "perfect"}, {id: "9", club: "ijzer7", afstand: "140", shot: "straight", uitvoering: "perfect"}];

var slag = data.map(doc => Object.values(doc));

console.log(slag);

hope this helps :)

Upvotes: 3

Related Questions