【问题标题】:determine location in a array [duplicate]确定数组中的位置[重复]
【发布时间】:2020-02-18 06:57:07
【问题描述】:

我正在尝试确定 a 在数组中的位置。我不确定如何处理这种情况

const expect = require('chai').expect;
const answers = require('../src/arrays');

describe('arrays', function() {
    let a;

    beforeEach(function() {
        a = [ 1, 2, 3, 4 ];
    });

    it('expect determine location of a in array', function(expect) {
        expect(answers.indexOf(a, 3)).to.eql(2);
        expect(answers.indexOf(a, 5)).to.eql(-1);

    });

【问题讨论】:

  • 我很确定“A”一定是“答案”中的一个元素?您在这里的用法不清楚。什么是答案?为什么要在“答案”中寻找数组?这就是你要寻找的东西吗?
  • 我添加了简单(数字数组)和复杂(对象数组)链接作为欺骗。为了更清楚,请参考两者

标签: javascript arrays


【解决方案1】:

试试这个!

var array = [ 1, 2, 3, 4 ]; 
   
function finding_index(element) { 
  return element === 2; 
} 
  
console.log(array.findIndex(finding_index));

【讨论】:

  • 虽然这很有帮助,但问题还不清楚。
猜你喜欢
  • 1970-01-01
  • 2013-09-22
  • 1970-01-01
  • 2020-04-05
  • 2013-06-12
  • 2012-07-16
  • 2013-07-18
  • 1970-01-01
  • 2014-10-15
相关资源
最近更新 更多