我应该如何实现一个函数来查找数组中非真实元素的索引?

发布于 2024-10-24 05:12:53 字数 575 浏览 6 评论 0原文

我有一个名为 uncompletedSteps() 的函数:

function uncompletedSteps(completedSteps) {
    // completedSteps is an array
}

该函数应检查 completedSteps 并返回所有不等于 completedSteps 元素的索引>true:

if (completedSteps[i] === true) {
    // add to return list
}

换句话说,如果有:

var completedSteps = [
    true,
    null,
    true
];

uncompletedSteps() 应返回 [0, 2]

这个 uncompletedSteps() 应该是什么样子? (ECMAScript5 可以。)

I have a function named uncompletedSteps():

function uncompletedSteps(completedSteps) {
    // completedSteps is an array
}

This function should examine completedSteps and return the index of all completedSteps elements that are not equal to true:

if (completedSteps[i] === true) {
    // add to return list
}

In other words, if have:

var completedSteps = [
    true,
    null,
    true
];

Then uncompletedSteps() should return [0, 2].

What should this uncompletedSteps() look like? (ECMAScript5 okay.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦醒灬来后我 2024-10-31 05:12:53

使用reduce

function uncompletedSteps(steps){
   return steps.reduce(function(memo, entry, i) { 
      return memo.concat(entry ? i : []);
   }, [])
}

使用forEach

function uncompletedSteps(steps){
   var uncompleted = [];
   steps.forEach(function(entry,i) { 
      if(entry) uncompleted.push(i); 
   })
   return uncompleted;
}

使用mapfilter

function uncompletedSteps(steps){
   return steps.map(function(entry, i) {
      return entry ? i : null;
   }).filter(function(entry) {
      return entry != null;
   });
}

Using reduce:

function uncompletedSteps(steps){
   return steps.reduce(function(memo, entry, i) { 
      return memo.concat(entry ? i : []);
   }, [])
}

Using forEach:

function uncompletedSteps(steps){
   var uncompleted = [];
   steps.forEach(function(entry,i) { 
      if(entry) uncompleted.push(i); 
   })
   return uncompleted;
}

Using map and filter

function uncompletedSteps(steps){
   return steps.map(function(entry, i) {
      return entry ? i : null;
   }).filter(function(entry) {
      return entry != null;
   });
}
活泼老夫 2024-10-31 05:12:53
var count = [];
for ( var i = 0; i<completedSteps.length; i++ ) {
  if(completedSteps[i]) {
    count.push(i);
  }
}
return count;
var count = [];
for ( var i = 0; i<completedSteps.length; i++ ) {
  if(completedSteps[i]) {
    count.push(i);
  }
}
return count;
摇划花蜜的午后 2024-10-31 05:12:53
var arr = [true, true, null, true];
arr.map(function(el, i) { 
    return el ? i : -1; 
}).filter(function(el) {
    return el != -1;
})

返回:

 [0, 1, 3]
var arr = [true, true, null, true];
arr.map(function(el, i) { 
    return el ? i : -1; 
}).filter(function(el) {
    return el != -1;
})

Returns:

 [0, 1, 3]
灯下孤影 2024-10-31 05:12:53

此函数应检查completedSteps 并返回所有不等于true 的completedSteps 元素的索引:

使用以下过程实现向后兼容性:

  • 多次替换以插入字符串null作为值转换为空字符串
  • 一个replace以删除true< /code>
  • another replace 使用替换回调插入索引
  • another replace 用于删除前导逗号
  • another replace 用于删除配对逗号

例如:

function foo(match, offset, fullstring)
  {
  foo.i = foo.i + 1 || 0;
  if (match === "true") 
    {
    return "";
    }
  else
    {
    return foo.i;
    }
  }

function uncompletedSteps(node)
  {
  return String(node).replace(/^,/ , "null,").replace(/,$/ , ",null").replace(/,,/g , ",null,").replace(/[^,]+/g, foo).replace(/^,/,"").replace(/,,/g,",")
  }

var completedSteps = [
    null,
    true,
    false,
    true,
    null,
    false,
    true,
    null
];

uncompletedSteps(completedSteps); // "0,2,4,5,7"

This function should examine completedSteps and return the index of all completedSteps elements that are not equal to true:

Use the following process for backwards compatibility:

  • multiple replaces to insert the string null for values converted to empty strings
  • one replace to remove true
  • another replace with a replacer callback to insert indices
  • another replace to remove a leading comma
  • another replace to remove paired commas

For example:

function foo(match, offset, fullstring)
  {
  foo.i = foo.i + 1 || 0;
  if (match === "true") 
    {
    return "";
    }
  else
    {
    return foo.i;
    }
  }

function uncompletedSteps(node)
  {
  return String(node).replace(/^,/ , "null,").replace(/,$/ , ",null").replace(/,,/g , ",null,").replace(/[^,]+/g, foo).replace(/^,/,"").replace(/,,/g,",")
  }

var completedSteps = [
    null,
    true,
    false,
    true,
    null,
    false,
    true,
    null
];

uncompletedSteps(completedSteps); // "0,2,4,5,7"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文