Javascriptdagi qiymatni qanday yig'ish mumkin?

Javascriptda shunga o'xshash qator mavjud. Shunga o'xshash narsa

[
  {
    "id": 1,
    "facilities": [
      {
        "id": 10,
        "name": "Wifi",
        "label": "Wifi"
      },
      {
        "id": 12,
        "name": "Toll",
        "label": "Toll"
      }
    ]
  },
  {
    "id": 2,
    "facilities": [
      {
        "id": 10,
        "name": "Wifi",
        "label": "Wifi"
      },
      {
        "id": 12,
        "name": "Toll",
        "label": "Toll"
      },
      {
        "id": 13,
        "name": "Snack",
        "label": "Snack"
      }
    ]
  },
  {
    "id": 3,
    "facilities": [
      {
        "id": 10,
        "name": "Wifi",
        "label": "Wifi"
      },
      {
        "id": 12,
        "name": "Toll",
        "label": "Toll"
      },
      {
        "id": 14,
        "name": "Petrol",
        "label": "Petrol"
      }
    ]
  }
]

Jadvaldagi ma'lumotlarning to'planishini va ma'lumotlar majmuasini guruhlashni istayman, bunga o'xshash narsa.

"facilities": [
      {
        "id": 10,
        "name": "Wifi",
        "label": "Wifi"
      },
      {
        "id": 12,
        "name": "Toll",
        "label": "Toll"
      },
      {
        "id": 13,
        "name": "Snack",
        "label": "Snack"
      },
      {
        "id": 14,
        "name": "Petrol",
        "label": "Petrol"
      }
    ]

Shunday qilib, asosan, ob'ektlar bo'yicha guruh. Men shunga o'xshash ob'ektlar qiymatlarini guruhlashni qanday boshqarishni bilmayman.

0

6 javoblar

id s obyekti mavjudligini inobatga olgan holda:

const facilities = input.reduce((memo, entry) => {
  entry.facilities.forEach((f) => {
    if (!memo.some((m) => m.id === f.id)) {
      memo.push(f)
    }
  })
  return memo
}, [])
1
qo'shib qo'ydi

Array foydalanib, yechim .prototype.reduce() va To'siq obyekti:

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

var data = [{"id": 1,"facilities": [{"id": 10,"name": "Wifi","label": "Wifi"},{"id": 12,"name": "Toll","label": "Toll"}]},{"id": 2,"facilities": [{"id": 10,"name": "Wifi","label": "Wifi"},{"id": 12,"name": "Toll","label": "Toll"},{"id": 13,"name": "Snack","label": "Snack"}]},{"id": 3,"facilities": [{"id": 10,"name": "Wifi","label": "Wifi"},{"id": 12,"name": "Toll","label": "Toll"},{"id": 14,"name": "Petrol","label": "Petrol"}]}
];

var ids = new Set(),
    result = data.reduce(function (r, o) {
        o.facilities.forEach(function(v) { //iterating through nested `facilities`
            if (!ids.has(v.id)) r.facilities.push(v);
            ids.add(v.id);  //saving only items with unique `id`
        });
        return r;
    }, {facilities: []});

console.log(result);
</div> </div>
0
qo'shib qo'ydi

Barcha satrlarda yineleyebilir va (id, shaxs) xaritasini to'plashingiz mumkin Indeks xaritasi har doim to'plangan sub'ektlarni qidirishni taqiqlaydi. Keyinchalik, uni ob'ekt tugmachalari xaritasi bilan bir qatorga o'zgartiradi.

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

let input = [
  {"id": 1, "facilities": [{"id": 10, "name": "Wifi", "label": "Wifi"}, {"id": 12, "name": "Toll", "label": "Toll"} ] },
  {"id": 2, "facilities": [{"id": 10, "name": "Wifi", "label": "Wifi"}, {"id": 12, "name": "Toll", "label": "Toll"}, {"id": 13, "name": "Snack", "label": "Snack"} ] },
  {"id": 3, "facilities": [{"id": 10, "name": "Wifi", "label": "Wifi"}, {"id": 12, "name": "Toll", "label": "Toll"}, {"id": 14, "name": "Petrol", "label": "Petrol"} ] }
];

let index = input.reduce((res, row) => {
  row.facilities.forEach(f => res[f.id] = f);
  return res;
}, {});

let result = Object.keys(index).map(id => index[id]);
console.log({facilities: result});
</div> </div>
0
qo'shib qo'ydi

Sozlash dan foydalanishingiz mumkin. berilgan id bilan kiritilgan moslamalarni belgilash uchun.

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

var data = [{ id: 1, facilities: [{ id: 10, name: "Wifi", label: "Wifi" }, { id: 12, name: "Toll", label: "Toll" }] }, { id: 2, facilities: [{ id: 10, name: "Wifi", label: "Wifi" }, { id: 12, name: "Toll", label: "Toll" }, { id: 13, name: "Snack", label: "Snack" }] }, { id: 3, facilities: [{ id: 10, name: "Wifi", label: "Wifi" }, { id: 12, name: "Toll", label: "Toll" }, { id: 14, name: "Petrol", label: "Petrol" }] }],
    grouped = data.reduce(
        (s => (r, a) => (a.facilities.forEach(b => !s.has(b.id) && s.add(b.id) && r.push(b)), r))(new Set),
        []
    );

console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }
</div> </div>
0
qo'shib qo'ydi

Juda sodda va oson tushunilgan yondashuv.

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="true"> <div class="snippet-code">

const data = [{
    "id": 1,
    "facilities": [{
        "id": 10,
        "name": "Wifi",
        "label": "Wifi"
      },
      {
        "id": 12,
        "name": "Toll",
        "label": "Toll"
      }
    ]
  },
  {
    "id": 2,
    "facilities": [{
        "id": 10,
        "name": "Wifi",
        "label": "Wifi"
      },
      {
        "id": 12,
        "name": "Toll",
        "label": "Toll"
      },
      {
        "id": 13,
        "name": "Snack",
        "label": "Snack"
      }
    ]
  },
  {
    "id": 2,
    "facilities": [{
        "id": 10,
        "name": "Wifi",
        "label": "Wifi"
      },
      {
        "id": 12,
        "name": "Toll",
        "label": "Toll"
      },
      {
        "id": 14,
        "name": "Petrol",
        "label": "Petrol"
      }
    ]
  }
];

let o = {};
let result = [];
data.forEach((d) => {
  d.facilities.forEach((f) => {
    o[f.id] = f;
  });
});

for (let r in o) {
  result.push(o[r]);
}

console.log(result);
</div> </div>
0
qo'shib qo'ydi

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

const input = [
  {
    "id": 1,
    "facilities": [
      {
        "id": 10,
        "name": "Wifi",
        "label": "Wifi"
      },
      {
        "id": 12,
        "name": "Toll",
        "label": "Toll"
      }
    ]
  },
  {
    "id": 2,
    "facilities": [
      {
        "id": 10,
        "name": "Wifi",
        "label": "Wifi"
      },
      {
        "id": 12,
        "name": "Toll",
        "label": "Toll"
      },
      {
        "id": 13,
        "name": "Snack",
        "label": "Snack"
      }
    ]
  },
  {
    "id": 3,
    "facilities": [
      {
        "id": 10,
        "name": "Wifi",
        "label": "Wifi"
      },
      {
        "id": 12,
        "name": "Toll",
        "label": "Toll"
      },
      {
        "id": 14,
        "name": "Petrol",
        "label": "Petrol"
      }
    ]
  }
]

const result = []
const idx = []

for (const item of input) {
  for (const facilityItem of item.facilities) {
    if (!idx.includes(facilityItem.id)) {
      idx.push(facilityItem.id)
      result.push(facilityItem) 
    }
  }
}

console.log(result)
</div> </div>
0
qo'shib qo'ydi
NodeJS Uzbekistan
NodeJS Uzbekistan
152 ishtirokchilar

Guruh NodeJS bo'yicha muloqot qilish uchun ochilgan. Sizni qiziqtirsa: @nodejs_uz @react_uz @angular_uz @yiiframework_uz @js_uzb @typescript_uzb @vuejs_uz @ngTashkent @laravel_uz @uzdevgroup Ish o'rinlari @UzDev_jobs

Javascript UZB
Javascript UZB
99 ishtirokchilar

@js_uzb @vuejs_uz @react_uz @nodejs_uz @angular_uz @ngTashkent @yiiframework_uz @laravel_uz @linux_uzbek @python_uz @swift_uzb —————— @uzdevgroup @UzGeeksGroup ——— @UzDev_Jobs @jobs_uzb