如何使用另一个元素保存数组中元素的位置?

问题描述 投票:-1回答:3

我的问题比现在更清楚有点困难,但我会在这里尝试更好地解释它。

我有三个数组:

var nums = [431, 324, 4344, 4324, 4344, 4324, 4444, 3441, 3214, 4344]
var nums4 = [324, 4344, 4324, 4344, 4324, 4444, 3214, 4344]
var posiciones = [];

我希望使用nums4上的每个元素检查nums的每个元素,并将这些元素在nums上的位置推入posiciones。

这是我到目前为止所做的,但我不知道为什么它不起作用:

var nums = [431, 324, 4344, 4324, 4344, 4324, 4444, 3441, 3214, 4344]
var nums4 = [324, 4344, 4324, 4344, 4324, 4444, 3214, 4344]
var posiciones = [];
    var pos = nums.indexOf(nums4[0])

    for (var io = 0; pos != -1 ;)
    /*while (pos != -1)*/
    {
        posiciones.push(pos);
        if(nums4.indexOf(nums4[io], pos + 1) == -1)
        {
        io++;
        }
        pos = nums.indexOf(nums4[io],pos + 1);
    }

    for(var i = 0; i < posiciones.length; i++)
    {
        posiciones[i]++;
    }

我不知道我是否清楚这个问题,因为英语不是我的母语,但如果你能解释我怎么做,我将不仅仅是感激。

编辑:答案应该是:

var posiciones = [2, 3, 4, 5, 6, 7, 9, 10]

但代码提供的答案不是:

var posiciones = [2, 3, 5, 10]

此外,我编辑了提供的数字,因为根据数字和答案提供,我寻找的数字不能给我所需的答案。

javascript arrays
3个回答
1
投票

(更新了您的新数据集,但请注意您的预期结果是错误的,它应该是[1,2,3,4,5,6,8,9],除非您的逻辑与您所​​说的非常非常不同,请参阅this comment。如果您希望第一个条目位于“index” “1而不是索引0,在下面的示例中保存索引时只使用+ 1

我希望使用nums4上的每个元素检查nums的每个元素,并将这些元素在nums上的位置推入posiciones。

对于这么小的数据集,对于nums4检查,我可能只是使用includes。 (对于更大的数据集,我会制作一个Map或类似的,以避免不断重新扫描。)

对于主循环,也许forEach

var nums = [431, 324, 4344, 4324, 4344, 4324, 4444, 3441, 3214, 4344]
var nums4 = [324, 4344, 4324, 4344, 4324, 4444, 3214, 4344]
var posiciones = [];
nums.forEach((value, index) => {
    if (nums4.includes(value)) {
        posiciones.push(index);
    }
});

console.log(posiciones);

或者以“index”1而不是0开头:

var nums = [431, 324, 4344, 4324, 4344, 4324, 4444, 3441, 3214, 4344]
var nums4 = [324, 4344, 4324, 4344, 4324, 4444, 3214, 4344]
var posiciones = [];
nums.forEach((value, index) => {
    if (nums4.includes(value)) {
        posiciones.push(index + 1); // + 1 to make it 1-based
    }
});

console.log(posiciones);

或者只是一个简单的for循环:

var nums = [431, 324, 4344, 4324, 4344, 4324, 4444, 3441, 3214, 4344]
var nums4 = [324, 4344, 4324, 4344, 4324, 4444, 3214, 4344]
var posiciones = [];
for (let index = 0; index < nums.length; ++index) {
    if (nums4.includes(nums[index])) {
        posiciones.push(index);
    }
}

console.log(posiciones);

或者以“index”1而不是0开头:

var nums = [431, 324, 4344, 4324, 4344, 4324, 4444, 3441, 3214, 4344]
var nums4 = [324, 4344, 4324, 4344, 4324, 4444, 3214, 4344]
var posiciones = [];
for (let index = 0; index < nums.length; ++index) {
    if (nums4.includes(nums[index])) {
        posiciones.push(index + 1); // + 1 to make it 1-based
    }
}

console.log(posiciones);

或者如果你想去所有的FP,mapfilter

var nums = [431, 324, 4344, 4324, 4344, 4324, 4444, 3441, 3214, 4344]
var nums4 = [324, 4344, 4324, 4344, 4324, 4444, 3214, 4344]
var posiciones = nums.map((value, index) => nums4.includes(value) ? index : null).filter(v => v !== null);

console.log(posiciones);

或者以“index”1而不是0开头:

var nums = [431, 324, 4344, 4324, 4344, 4324, 4444, 3441, 3214, 4344]
var nums4 = [324, 4344, 4324, 4344, 4324, 4444, 3214, 4344]
var posiciones = nums.map((value, index) => nums4.includes(value) ? index + 1 : null).filter(v => v !== null);
// + 1 to make it 1-based -----------------------------------------------^^^^

console.log(posiciones);

0
投票

如果在nums中找不到nums4元素,则会得到-1。

var nums = [10, 3, 45, 44, 34, 4, 23, 44, 23, 254]
var nums4 = [44, 34, 4, 44, 254]
var posiciones = nums4.map(n => nums.indexOf(n));

console.log(posiciones);

0
投票

你可以采取一个Map并映射转移的指数。

var nums = [431, 324, 4344, 4324, 4344, 4324, 4444, 3441, 3214, 4344],
    nums4 = [324, 4344, 4324, 4344, 4324, 4444, 3214, 4344],
    pos = nums.reduce((m, v, i) => m.set(v, [...(m.get(v) || []), i + 1]), new Map);
    posiciones = nums4.map(v => pos.get(v).shift());

console.log(...posiciones);
© www.soinside.com 2019 - 2024. All rights reserved.