简体   繁体   中英

Return array of strings and numbers from parsed object

Trying to create a function to remove duplicates from an array of strings, numbers, and booleans by adding array values to an object as the keys and then pushing the keys to a result array. I know I can write it much more quickly and cleanly with a callback and ".filer" / ".indexOf" but I'm trying to write it without.

const removeDuplicates = (arr) => {
  let result = [];
  let obj = {};
  for(let i = 0; i < arr.length; i++) {
    obj[arr[i]] = "";
  }
  for(key in obj) {
    if(typeof key === 'number'){
      result.push(parseInt(key));
    }else{
      result.push(key);
    }
  }
  return result;
}

const myArr = [1,2,2,"hello","hello",3,3,"goodbye","goodbye",4,4,4,true,true,5,5];
console.log(removeDuplicates(myArr));

This function returns:

["1", "2", "3", "4", "5", "hello", "goodbye", "true"]

I am expecting:

[1, 2, 3, 4, 5, "hello", "goodbye", "true"];

Not sure why my "parseInt" isn't working. I suspect it has something to do with how I'm using "typeOf". Or possibly when I'm adding array values to the object they are being saved in the object keys as strings. Any help is appreciated.

const myArr = [1,2,2,"hello","hello",3,3,"goodbye","goodbye",4,4,4,true,true,5,5];
let obj = {}
const newArr = myArr.reduce((arr, item) => {
    if(!object.hasOwnProperty(item)){
        object[item] = 0;
        arr.push(item);
    }
    return arr;
}, []);

console.log(newArr) //[1, 2, "hello", 3, "goodbye", 4, true, 5];

If the order matters you can do return arr.sort()

You check the key of an object, this is always a string, the check does not work, but you could convert the value to a number and back to a string and check it with the key. If true , you have a numerical value.

  const removeDuplicates = (arr) => { let result = []; let obj = {}; for (let i = 0; i < arr.length; i++) { obj[arr[i]] = ""; } for (key in obj) { if ((+key).toString() === key) { result.push(+key); } else { result.push(key); } } return result; } const myArr = [1, 2, 2, "hello", "hello", 3, 3, "goodbye", "goodbye", 4, 4, 4, true, true, 5, 5]; console.log(removeDuplicates(myArr)); 

Just for the records, you could use a Set , which gives unique results.

 const array = [1, 2, 2, "hello", "hello", 3, 3, "goodbye", "goodbye", 4, 4, 4, true, true, 5, 5], unique = [...new Set(array)]; console.log(unique); // in order of appearance 

First of all your this condition if(typeof key === 'number') is never satisfied, they are all of type string .

Second, instead of writing this whole block

for(key in obj) {
    if(typeof key === 'number'){
      result.push(parseInt(key));
    }else{
      result.push(key);
    }
  }
  return result;

You can just return Object.keys(obj) .

Now where ever you want to check for string vs number just do (+index).toString() === index)

It works properly:

  const removeDuplicates = (arr) => {
  let result = [];
  let s = new Set()
  for (key in arr)
  {
    if (!s.has(arr[key])) {
        result.push(arr[key])
        s.add(arr[key])
    }
  }
  return result;
}

You can use JavaScript built in object Set to keep unique values of any type. Here is how code looks.

function removeDuplicates(arr){
    let result = Array.from(new Set(arr))
    return result;
}

const arr = [1,2,2,"hello","hello",3,3,"goodbye","goodbye",4,4,4,true,true,5,5];
console.log(removeDuplicates(arr));

This will give you your expected result. Let me know if it worked or not. :)

const removeDuplicates = (arr) => {
        let result = [];
        let obj = {};
        for(let i = 0; i < arr.length; i++) {
            obj[arr[i]] = "";
        }
        for(key in obj) {
            if(isNaN(+key)){
                result.push(key);
            }else{
                result.push(+key);
            }

        }
        return result;
    }

    const myArr = [1,2,2,"hello","hello",3,3,"goodbye","goodbye",4,4,4,true,true,5,5];
    console.log(removeDuplicates(myArr));

typeOf "2" will return string not a number thats why your code is not working.You need to try to parse it into int and then decide. Hope this helps!!!

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