Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

169
Views
how to build a json array with custom elements with javascript

I'm using JavaScript, I have an array:

[
  { "id": "0", "name": "test1", "brand": "brand1" },
  { "id": "0", "name": "test1", "brand": "brand2" },
  { "id": "0", "name": "test1", "brand": "brand3" },
  { "id": "2", "name": "test2", "brand": "brand100" },
  { "id": "2", "name": "test2", "brand": "brand101" },
  { "id": "2", "name": "test2", "brand": "brand102" },
  { "id": "2", "name": "test2", "brand": "brand103" },
  { "id": "2", "name": "test2", "brand": "brand104" }
]

I'm using JavaScript to have something like this:

[
  {
    "id": "0",
    "name": "test1",
    "brand": [
      "brand1",
      "brand2",
      "brand3"
    ]
  },
  {
    "id": "2",
    "name": "test2",
    "brand": [
      "brand100",
      "brand101",
      "brand102",
      "brand103",
      "brand104"
    ]
  }
]

The script is:

let result=[]
for (var i = 0; i < brands.length; i++) {
let id =brands[i].id
    if(i==0){

        result.push(

          {
          "id": brands[i].id,
          "name": brands[i].name, 
           } )
 else{
        result.push(
           "id": brands[i].id,
          "name": brands[i].name )
    }
}

But it seems that it doesn't work...?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You could take an object for the obj3ects with same id and get all values of it as result;

const
    data = [{ id: "0", name: "test1", brand: "brand1" }, { id: "0", name: "test1", brand: "brand2" }, { id: "0", name: "test1", brand: "brand3" }, { id: "2", name: "test2", brand: "brand100" }, { id: "2", name: "test2", brand: "brand101" }, { id: "2", name: "test2", brand: "brand102" }, { id: "2", name: "test2", brand: "brand103" }, { id: "2", name: "test2", brand: "brand104" }],
    result = Object.values(data.reduce((r, { id, name, brand }) => (
        (r[id] ??= { id, name, brand: [] }).brand.push(brand),
        r
    ), {}));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

You have some syntax problems in your code (You have missed brackets). Here is a fixed one:

let result = []
for (var i = 0; i < brands.length; i++) {
    let id = brands[i].id
    if (i == 0) {
        result.push({
            "id": brands[i].id,
            "name": brands[i].name,
        });
    } else {
        result.push({
            "id": brands[i].id,
            "name": brands[i].name
        });
    }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!