Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
326 views
in Technique[技术] by (71.8m points)

javascript - 为了避免索引而使用Array.map是否值得?(Is it worth it to use Array.map just to avoid indexing?)

Consider the following code snippet:

(考虑以下代码片段:)

function getMaxAge(arr, id, max) {
  let found = false
  for (let i = 0; i < arr.length; i++) {
    const val = arr[i];
    if (val.id === id) {
      found = true
      if (max > val.age) val.age = max
    }
    return val
  })

  if (found === false) {
    arr.push({
      id: id,
      age: max
    })
  }
  return arr
}

Now watch the same logic using map:

(现在,使用map观察相同的逻辑:)

function getMaxAge(arr, id, max) {
  let found = false
  arr = arr.map((val, index) => {
    if (val.id === id) {
      found = true
      if (max > val.age) val.age = max
    }
    return val
  })

  if (found === false) {
    arr.push({
      id: id,
      age: max
    })
  }
  return arr
}

What I am interested in is not the logic of the function, but whether the use of Array.map is justified for this scenario?

(我感兴趣的不是函数的逻辑,而是在这种情况下使用Array.map是否合理?)

  ask by faint-hearted-fool translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I personally avoid manual iteration whenever possible.

(我个人尽可能避免手动迭代。)

Iterator methods are a concept build upon manual iteration and let objects themself decide how iteration is done.

(迭代器方法是基于手动迭代的概念,可以让对象自己决定如何进行迭代。)

So if you have the option, I'd say avoid manual iteration.

(因此,如果您有选择,我会避免手动迭代。)

There are a few things to keep in mind.

(有几件事要牢记。)

Generally speaking I would avoid iteration methods when altering the collection while iterating.

(一般来说,在迭代时更改集合时,我会避免使用迭代方法。)

Because you don't know the implementation you don't fully know what's happening when adding/removing elements to the collection during iteration.

(因为您不知道实现,所以您不完全知道在迭代过程中向集合中添加/删除元素时发生了什么。)

When you explicitly need to work with the index (and not the element) it might be worth the trouble to manually do iteration.

(当您明确需要使用索引(而不是元素)时,手动进行迭代可能会很麻烦。)

With the above being said, it's good to know what iteration methods are available to you, since different methods fulfil different tasks.

(综上所述,最好知道可用的迭代方法,因为不同的方法可以完成不同的任务。)

Knowing which method to use in which scenario will result in better performance and cleaner code.

(知道在哪种情况下使用哪种方法将导致更好的性能和更简洁的代码。)

Taking your example I'd change it to the following:

(以您的示例为例,我将其更改为以下内容:)

function getMaxAge(arr, id, max) {
  arr = arr.slice();
  const matchesId = arr.filter(val => val.id == id),
        found = matchId.length;

  matchesId.filter(val => val.age < max)
           .forEach(val => (val.age = max));

  if (!found) arr.push({id: id, age: max});
  return arr;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...