-1

Say I have a giant string with lots of dogs in it. I was thinking of using .split at the every " " and writing a loop that checks each name to see if its a match with the same property name. I want to do this with minimal if statements and assignment considering how it's a very big string with many types of dogs. so now after I split each dog apart. I don't want to have to write an if statement for each dog is there some way around this? So because the tag is = to the string can i automatically make it go in there





const occurrences = function(str){
str.split(" ")

for(var i=0, c=0; i > str.length; i++){str.split(" ")
    if str[i]==="pug"
}

 let dogs= {[ "weiner":,
        "pug": ,
        "daschhound":,
        "dog":,
});

return dogs}

const myDogs= occurrences("dog dog dog dog dog dog dog dog"
"dog dog dog dog dog dog dog pug")

returns {"pug":"pug"
"dog": "dog" "dog""dog" "dog" "dog" "dog" "dog" "dog" 
"daschhound":
}

// imagine a huge list of 200 different breeds is there some way to assign each of them to the property of the same name.

1 Answers1

1

Not really clear about your requirement, but check if this helps

const occurences = str => {
    let dogs = {};
    str.split(' ').forEach(word => dogs[word] ? dogs[word] += ` ${word}` : dogs[word] = word);
    return dogs;
}

const str = 'dog dog dog pug jon';
console.log(occurences(str)); // prints {dog: "dog dog dog", pug: "pug", jon: "jon"}
Abito Prakash
  • 4,368
  • 2
  • 13
  • 26