简体   繁体   English

检查数值是否在 object 内

[英]checking if number value is within an object

I'm trying to map each number in the array as a key to the empty object, and set the value as the index.我正在尝试将 map 数组中的每个数字作为空 object 的键,并将该值设置为索引。 However, the first index, 0, gets skipped...?但是,第一个索引 0 被跳过...?

function solve(nums) {

    let obj = {};
    for (let i=0; i<nums.length; i++) {
        if (!obj[nums[i]]) {  // if key does not exist, set key/value pair 
            obj[nums[i]] = i
        } else if (obj[nums[i]]) {
            obj[nums[i]] = 0  // if key exists, set to 0 
        }

    }

    console.log(obj)
}
solve([50, 30, 50, 90, 10])

You could use Map instead.您可以改用Map Something like this:像这样的东西:

function solve(nums) {
    let obj = new Map();
    for (let i=0; i<nums.length; i++) {
        if (obj.get(nums[i]) == undefined) {  // if key does not exist, set key/value pair
            obj.set(nums[i]) = i;
        } else {
            obj.set(nums[i], 0);  // if key exists, set to 0 
        }
    }
    console.log(obj);
}
solve([50, 30, 50, 90, 10]);

UPDATE更新

It seems what you really need is something different.看来你真正需要的是不同的东西。 I will try to explain it a bit better since you have 2 numbers having the same value 50 and the only thing that's unique is the index, then you could have the index as the key, but also, you could need just one value and having both indexes as a value.我将尝试更好地解释它,因为您有 2 个具有相同值50的数字,唯一唯一的是索引,那么您可以将索引作为键,但是,您可能只需要一个值并具有两个索引都作为一个值。

For the first option you could use this:对于第一个选项,您可以使用:

function solve(nums) {
    let obj = new Map();
    for (let i=0; i<nums.length; i++) {
        obj.set(i,nums[i]);
    }
    console.log(obj);
}
solve([50, 30, 50, 90, 10]);

For the second option this would be the code:对于第二个选项,这将是代码:

function solve(nums) {
    let obj = new Map();
    for (let i=0; i<nums.length; i++) {
        if (obj.get(nums[i]) == undefined) {  // if key does not exist, set key/value pair 
            obj.set(nums[i]) = i;
        } else {
            obj.set(nums[i], obj.get(nums[i]) + ',' + i);  // if key exists, append the second index 
        }
    }
    console.log(obj);
}
solve([50, 30, 50, 90, 10]);

Everything is ok.一切都好。 It does not get skipped.它不会被跳过。 You pass 50 two time but Object can not have two same properties.您两次通过50Object不能有两个相同的属性。 So second 50 is overriding the first value .所以第二个50覆盖了第一个value

When you call solve([500, 30, 50, 90, 10])当您调用solve([500, 30, 50, 90, 10])

You will get {10: 4 30: 1 50: 2 90: 3 500: 0} Which is expected result.你会得到{10: 4 30: 1 50: 2 90: 3 500: 0}这是预期的结果。

First of all, I am confused a bit with else if block, why you set it to 0 , if you find same number again.首先,我对else if块有点困惑,为什么你将它设置为0 ,如果你再次找到相同的数字。

But it is skipped, because you have 50 two times in your array.但它被跳过了,因为你的数组中有两次50 And when you loop through nums, when you get to index i = 2 , you will have in object { 50: 0, 30: 1} .当您遍历 nums 时,当您到达索引i = 2时,您将拥有 object { 50: 0, 30: 1}

And because of that, !0 returns true , so you will get again in if block, and you will override your value for key 50 with 2 .正因为如此, !0返回true ,因此您将再次进入if块,并且您将使用2覆盖键50的值。

Try with an array with 5 different values and it will work as you expect it尝试使用具有 5 个不同值的数组,它将按预期工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM