最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using Find & ForEach in JS Together - Stack Overflow

programmeradmin0浏览0评论

I have two arrays called active_filters and cars. I'm working on improving performance and I'm finding that the code below is super heavy and slow. I'm wondering if it's possible to pare x.value and element[0] from cars array without having the outer forEach? I'm super new to this, so appreciate any help! If there's a better way of going about this too, I'm open to suggestions :) Thanks!!!

 cars.forEach(function(element) {
    active_filters.find(x => x.value === element[0].getAttribute("data-location-id"));
 });

Update:

My goal is to return whether or not the attribute data-location-id (which is located in the cars array) is located and equal to the value property in the active_filters array.

Sample Data:

Active Filters Array Object

{class: "data-location-id", value: "AD-48.7284-2.3601"}
{class: "data-location-id", value: "AL-48.726243-2.365247"}

Cars Array:

<li data-location-id="ZT-48.8308-2.3567" class="listing"></li>
<li data-location-id="AD-48.7284-2.3601" class="listing"></li>
<li data-location-id="AC-28.7284-2.3601" class="listing"></li>

I have two arrays called active_filters and cars. I'm working on improving performance and I'm finding that the code below is super heavy and slow. I'm wondering if it's possible to pare x.value and element[0] from cars array without having the outer forEach? I'm super new to this, so appreciate any help! If there's a better way of going about this too, I'm open to suggestions :) Thanks!!!

 cars.forEach(function(element) {
    active_filters.find(x => x.value === element[0].getAttribute("data-location-id"));
 });

Update:

My goal is to return whether or not the attribute data-location-id (which is located in the cars array) is located and equal to the value property in the active_filters array.

Sample Data:

Active Filters Array Object

{class: "data-location-id", value: "AD-48.7284-2.3601"}
{class: "data-location-id", value: "AL-48.726243-2.365247"}

Cars Array:

<li data-location-id="ZT-48.8308-2.3567" class="listing"></li>
<li data-location-id="AD-48.7284-2.3601" class="listing"></li>
<li data-location-id="AC-28.7284-2.3601" class="listing"></li>
Share Improve this question edited May 4, 2018 at 0:07 Barmar 784k57 gold badges548 silver badges659 bronze badges asked May 3, 2018 at 23:38 MichaelMichael 4431 gold badge11 silver badges32 bronze badges 10
  • 1 can you provide more detail? – sumit Commented May 3, 2018 at 23:40
  • What are you trying to achieve? – PM 77-1 Commented May 3, 2018 at 23:40
  • Some sample data would be useful. – fubar Commented May 3, 2018 at 23:41
  • what do expect return results ? true/false ? matched car ? – Muhammad Usman Commented May 3, 2018 at 23:42
  • I provided an update to my post. Expected results are true/false – Michael Commented May 3, 2018 at 23:43
 |  Show 5 more ments

2 Answers 2

Reset to default 2

To get a result for each element, you use map(), not forEach(). They both iterate over the array and call the function, but forEach() discards the result, while map() collects all the results into an array.

cars.map(car => !!active_filters.find(x => x.value === car.data("location-id")));

!! turns the result of find() into a boolean true/false.

To reduce your time plexity to linear, you can create an object with the "data-location-id" as the keys from the active_filters array. This will give you the ability to do a constant time lookup and just use a single loop instead of nested loops.

const active_filters = [
  { class: "data-location-id", value: "AD-48.7284-2.3601" },
  { class: "data-location-id", value: "AL-48.726243-2.365247" }
];

// from active filters, create this active_filtersById object 
// with the value as keys for constant time lookup

const active_filtersById = {
  "AD-48.7284-2.3601": { class: "data-location-id", value: "AD-48.7284-2.3601" },
  "AL-48.726243-2.365247": { class: "data-location-id", value: "AL-48.726243-2.365247" }
}

const cars = [
  { "data-location-id": "ZT-48.8308-2.3567" },
  { "data-location-id": "AD-48.7284-2.3601" },
  { "data-location-id": "AC-28.7284-2.3601" }
];

   
const result = cars.map(car => {
  const id = car["data-location-id"];
  const isFound = active_filtersById[id];
  return isFound !== undefined;
})

console.log(result)
// result is [false, true, false]

发布评论

评论列表(0)

  1. 暂无评论