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

158
Views
Quickly toggle a boolean property of objects in an array based on a list of attribute values

I have an array of objects like:

[{type: 'marker', name: 'somename', id: '123abc321', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}, {type: 'marker', name: 'somename', id: '123abc322', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}, {type: 'marker', name: 'somename', id: '123abc323', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}]

The maximum number of objects in the array is around 7600

I am struggling to find a fast way of flipping the 'selected' property from true to false or vice versa of every object whose id is included in an array of id values like:

['123abc322', '123abc323']

The supplier array of id values could include ALL the object ids and will often be ~2000 ids.

So if the object.id field is in the supplied array I want to swap the object.selected to !object.selected

Any help would be appreciated.

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

0

I would recommend converting your array of IDs into a Set. Then, it's a matter of doing one pass over your array and, if the Set contains the element's ID, toggle the selected property.

Note that this method modifies the objects in the array. If you need to not mutate the input, you could use a map.

Mutating the input:

const arr = [{type: 'marker', name: 'somename', id: '123abc321', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}, {type: 'marker', name: 'somename', id: '123abc322', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}, {type: 'marker', name: 'somename', id: '123abc323', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}];

const ids = new Set(['123abc322', '123abc323']);

arr.forEach(el => {
  if (ids.has(el.id)) {
    el.selected = !el.selected;
  }
});

console.log(arr);

Not mutating the input:

const arr = [{type: 'marker', name: 'somename', id: '123abc321', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}, {type: 'marker', name: 'somename', id: '123abc322', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}, {type: 'marker', name: 'somename', id: '123abc323', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}];

const ids = new Set(['123abc322', '123abc323']);

const newArr = arr.map(el => {
  if (!ids.has(el.id)) return el;

  return {
    ...el,
    selected: !el.selected
  }
});

console.log(newArr);

about 4 years ago · Juan Pablo Isaza Report

0

You can do everything with a single line of code, here's a code example:

let array = [{
    type: "marker",
    name: "somename",
    id: "123abc321",
    lat: 123.0,
    lng: 32.0,
    educationRegion: "someregion",
    seDistrict: "sometype",
    sector: "somesector",
    selected: false,
    shownOnMap: true,
    listed: false,
  },
  {
    type: "marker",
    name: "somename",
    id: "123abc322",
    lat: 123.0,
    lng: 32.0,
    educationRegion: "someregion",
    seDistrict: "sometype",
    sector: "somesector",
    selected: false,
    shownOnMap: true,
    listed: false,
  },
  {
    type: "marker",
    name: "somename",
    id: "123abc323",
    lat: 123.0,
    lng: 32.0,
    educationRegion: "someregion",
    seDistrict: "sometype",
    sector: "somesector",
    selected: false,
    shownOnMap: true,
    listed: false,
  },
];

let ids = ['123abc322', '123abc323'];

// .filter() create a new array with all elements that pass the test implemented by the provided function.
// .map() run a function for everyone of the remaining objects and flip the "selected" value.

array.filter(x => ids.includes(x.id)).map(x => x.selected = !x.selected);
console.log(array);

Hope It's what your looking for.

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!