简体   繁体   中英

Set array value as a object key

I found this code what count the repeated elements in array

 const a = ['first', 'first', 'second']; const obj = {}; for (let i = 0; i < a.length; i++) { const current = a[i]; if (obj[current]) { obj[current] += 1 }else { obj[current] = 1 } } console.log(obj)

I have problems to figure out how this if statement creates the object. I supose that the main logic is here:

 if (obj[current]) {
        obj[current] += 1
    }else {
        obj[current] = 1
 }

The if statement chack if in obj exists a key, it increase the the number obj[current] += 1 , but how this code set the value from array as a key, because obj[current] output the number, not the key. How the code set the key for object?

if the current element in the array a does not yet exist in obj , then its default value is 1. This rough call stack chart may be helpful

i |   a[i]   |    obj
0 | 'first'  | { first: 1 }
1 | 'first'  | { first: 2 }
2 | 'second' | { first: 2, second: 1 }
var obj = {}

if (obj[current]) {
  obj[current] += 1;
} else {
  obj[current] = 1;
}

obj is a dictionary structure which is also called associative array .

Dictionary keys might be any type in general programming. But in javascript usually string or number types used as dictionary keys. You can get or set value inside dictionary using obj['key'] . You can also use variables to represent dictionary keys like: obj[current] .

 const obj = {} const keyName = 'stack' obj[keyName] = 'overflow'; console.log(obj); // { "stack": "overflow" } obj['stack'] = 'exchange'; console.log(obj); // { "stack": "exchange" }

The code you mentioned checks if obj[current] value is not any of null , 0 , "" , undefined or false . Dictionary[key] returns undefined if key is not available in dictionary.

You can also write this part like that:

if (obj[current] !== undefined)

So if (obj[current]) checks if obj dictionary has an key equals to current or not. If the key is available it simply increases value of dictionary item. If the key is not available it puts new item to obj dictionary with current key and 1 value.

I guess you misunderstand on this line

obj[current] output the number , not the key . How the code set the key for an object?

So if I add a console inside the for then you can easily see the values of obj[current] on each iteration based on i , it returns undefined when the obj doesn't contain the key(based on your if condition) and when it exists a number ie when obj[current] is not equal undefined , 0 , false , "" or null then you are incrementing it by one.

 const a = ['first', 'first', 'second']; const obj = {}; for (let i = 0; i < a.length; i++) { const current = a[i]; console.log("When current="+current+" then obj[current]="+obj[current]); if (obj[current]) { // when obj[current] not equal undefined, 0, false, "" or null obj[current] += 1 }else { obj[current] = 1 } } console.log(obj)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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