user1862965
user1862965

Reputation: 386

how to remove doube square brackets in json array - nodejs

Function 1 return array

var arra = [
   { TableTitel: 'Sockel', TableValue: 'AM4 (PGA)' },
   { TableTitel: 'Codename', TableValue: 'Matisse' },
   { TableTitel: 'iGPU', TableValue: 'N/​A' }
];

Json file:

var maindata = {
  podTitel: "AMD Ryzen 7 3700X, 8x 3.60GHz, boxed (100-100000071BOX)",
  podURL:
    "https://xml/amd-ryzen-7",
  podDesc:
    "Sockel: AM4 (PGA)",
  podStars: "4.9 von 5",
};

I want to create details array in Json maindata and put arra in details.

maindata.details = [];
maindata.details.push(arra);

Result is console.log(maindata);

{
  podTitel: 'AMD Ryzen 7 3700X, 8x 3.60GHz, boxed (100-100000071BOX)',
  podURL: 'https://xml/amd-ryzen-7',
  podDesc: 'Sockel: AM4 (PGA)',
  podStars: '4.9 von 5',
  details: [
    [
      { TableTitel: 'Sockel', TableValue: 'AM4 (PGA)' },
      { TableTitel: 'Codename', TableValue: 'Matisse' },
      { TableTitel: 'iGPU', TableValue: 'N/​A' }
    ]
  ]
}

Problem is: arra is array and details is also array, and details has after push() two square brackets. what is the best way to remove double square brackets.

This should be the result

 {
      podTitel: 'AMD Ryzen 7 3700X, 8x 3.60GHz, boxed (100-100000071BOX)',
      podURL: 'https://xml/amd-ryzen-7',
      podDesc: 'Sockel: AM4 (PGA)',
      podStars: '4.9 von 5',
      details: [

          { TableTitel: 'Sockel', TableValue: 'AM4 (PGA)' },
          { TableTitel: 'Codename', TableValue: 'Matisse' },
          { TableTitel: 'iGPU', TableValue: 'N/​A' }

      ]
    }

tnx a lot

Upvotes: 0

Views: 62

Answers (1)

Mohammad Faisal
Mohammad Faisal

Reputation: 2402

you can use this

maindata.detail = [...arra]; 

DEMO

var arra = [{
    TableTitel: 'Sockel',
    TableValue: 'AM4 (PGA)'
  },
  {
    TableTitel: 'Codename',
    TableValue: 'Matisse'
  },
  {
    TableTitel: 'iGPU',
    TableValue: 'N/​A'
  }
];


var maindata = {
  podTitel: "AMD Ryzen 7 3700X, 8x 3.60GHz, boxed (100-100000071BOX)",
  podURL: "https://xml/amd-ryzen-7",
  podDesc: "Sockel: AM4 (PGA)",
  podStars: "4.9 von 5",
};

maindata.detail = [...arra]; 

console.log(maindata);

Upvotes: 1

Related Questions