Home > Enterprise >  Remove First and Last Double Quote From JSON Array
Remove First and Last Double Quote From JSON Array

Time:01-24

Here i have one big array that has multiple arrays inside. I need to remove double quotes from each array. I tried all different methods to pull out quotes but none seem to work.

Current code produces this result:

[
  ["1,jone,matt,ny,none,doctor"],
  ["2,maria,lura,nj,some,engineer"],
  ["3,paul,kirk,la,none,artist"]
]

I need it this way:

[
  [1,jone,matt,ny,none,doctor],
  [2,maria,lura,nj,some,engineer],
  [3,paul,kirk,la,none,artist]
]
    const storeArray = [];

    for(var i = 0; i < results.length; i  ) {

      var finalArray = results[i].id   ","   results[i].name   ","   results[i].lastname   ","   results[i].address   ","   results[i].status   ","   results[i].about;

      storeArray.push([finalArray]);
    }

    res.send(storeArray);

CodePudding user response:

You're creating strings as the first element of an array instead of an array of elements. You'll still have to contend with quotes because some of your data are strings - there's no getting round that - but this is closer to what you want.

const results = [
  { id: 1, name: 'Andy', lastname: 'Jones', address: '999 Letsbe Avenue', status: 5, about: 'About' },
  { id: 2, name: 'Sue', lastname: 'Barlow', address: '1 Fifth Street', status: 1, about: 'Another about' }
];

const storeArray = [];

for (let i = 0; i < results.length; i  ) {

  storeArray.push([
    results[i].id,
    results[i].name,
    results[i].lastname,
    results[i].address,
    results[i].status,
    results[i].about
  ]);

}

console.log(storeArray);

CodePudding user response:

Using split(',') you can do that, try this code !

      let results = [
          ["1,jone,matt,ny,none,doctor"],
          ["2,maria,lura,nj,some,engineer"],
          ["3,paul,kirk,la,none,artist"]
        ]            
    
       let storeArray = results.map((val) => val[0].split(','));
       console.log(storeArray, 'storeArray');
       

  •  Tags:  
  • Related