Javascript - missing array

I need to write a JS function that creates an object from a given string. String characters are object characters. Repeated characters should not be included twice. The values ​​of all keys are zero.

Example: input objFromStr("aaabc")should return{a: 0, b: 0, c: 0}

Here is my solution:

function objFromStr(inStr) {
  let charsArr = inStr.split("");

  let charsObj = {};
  var alreadyInArr = [];

  for (var i = 0; i < charsArr.length; i++) {
    if (!(alreadyInArr.includes(charsArr[i]))) {
      charsObj[charsArr[i]] = 0;
    } else {
      alreadyInArr.push(charsArr[i]);
    }
  }

  return charsObj;
}

This solution works as expected, but I don't understand why. I check for duplicate characters with alreadyInArr. However, when I record my own alreadyInArr, it is empty.

So, after running this code:

function objFromStr(inStr) {
  let charsArr = inStr.split("");

  console.log("charsArr is:", charsArr);

  let charsObj = {};
  var alreadyInArr = [];

  for (var i = 0; i < charsArr.length; i++) {
    if (!(alreadyInArr.includes(charsArr[i]))) {
      charsObj[charsArr[i]] = 0;
    } else {
      alreadyInArr.push(charsArr[i]);
    }
  }

  console.log("alreadyInArr is:", alreadyInArr);

  return charsObj;
}

console.log(objFromStr("aaabc"));

My conclusion:

charsArr is: [ 'a', 'a', 'a', 'b', 'c' ]
alreadyInArr is: []
{ a: 0, b: 0, c: 0 }

Can someone explain why it is alreadyInArrempty, and yet the function still works as expected?

Here's the handle: https://codepen.io/t411tocreate/pen/bYReRj?editors=0012

+4
source share
6

else , , console.log() , , .

if, :

for (var i = 0; i < charsArr.length; i++) {
    if (!alreadyInArr.includes(charsArr[i])) {
        charsObj[charsArr[i]] = 0;
        alreadyInArr.push(charsArr[i]);
    }
}

, .

function objFromStr(inStr) {
  let charsArr = inStr.split("");

  console.log("charsArr is:", charsArr);

  let charsObj = {};
  var alreadyInArr = [];

  for (var i = 0; i < charsArr.length; i++) {
    if (!alreadyInArr.includes(charsArr[i])) {
      charsObj[charsArr[i]] = 0;
      alreadyInArr.push(charsArr[i]);
    }
  }

  console.log("alreadyInArr is:", alreadyInArr);

  return charsObj;
}

console.log(objFromStr("aaabc"));
Hide result
+3

:

const string = "aabbcc", result = {};

for(const char of string)
  result[char] = 0;
+3

else , , includes() true.

.

+2
 if (!(alreadyInArr.includes(charsArr[i]))) {
      charsObj[charsArr[i]] = 0;
    } else {
      alreadyInArr.push(charsArr[i]);
    }

alreadyInArr.includes !. : NOT ( Inarr CharrsArr [i] ), :

, , .

, ,

+2

You can distribute carcters and display objects with the desired properties.

function objFromStr(inStr) {
    return Object.assign(...Array.from(inStr, k => ({ [k]: 0 })));
}

console.log(objFromStr('aaaabbc'));
Run codeHide result
+2
source

As others have already pointed out, you do not allow access to the array so that you constantly rewrite your object and assign 0.

You can cut the code as follows:

var arr = "aabbc", obj = {};

function objFromStr(inStr) {        
    arr.split("").forEach(function(x){
        obj[x] !== undefined ? obj[x]++ : obj[x] = 1;
    });     
    return obj;
}

console.log(objFromStr(arr));

This way you do not need to click or track anything in a separate array. It will check obj to see if a key exists. If he does, he will add 1 to it, and if not, he will consider it equal to 1.

+1
source

Source: https://habr.com/ru/post/1689207/


All Articles