JS Turn an array into Object with property key/value

i was coding and i found this problem, the goal is turn the items array into a object with property key/value, counting the items that appear more than once like that:

{
  cookie:{
    MILK: 1,
    CHOCOLATE: 2,
    DELUXE: 1
  },
  bread:{
    BIG: 2
  },
  beer:{
    NEW: 1,
    OLD: 1
  }
}

I tried this code below

const items = [
  "cookie-MILK",
  "cookie-CHOCOLATE",
  "cookie-CHOCOLATE",
  "cookie-DELUXE",
  "bread-BIG",
  "bread-BIG",
  "beer-NEW",
  "beer-OLD"
]
let newArray = [];

items.forEach((e) => {
  let splitArray = e.split("-");
  newArray.push([splitArray[0], splitArray[1]]);
});


let result = newArray.reduce((acc, val) => {
  if (!acc[val[0]] && !acc[val[1]] ) acc[val[0]] = {
     [val[1]]: 1,
  };
   else acc[val[0]][val[1]]++;
  return acc;
}, {});

But this code returns it and i don’t know how to solve this question

{
  cookie:{
    MILK: 1,
    CHOCOLATE: NaN,
    DELUXE: NaN
  },
  bread:{
    BIG: 2
  },
  beer:{
    NEW: 1,
    OLD:  NaN
  }
}

178 thoughts on “JS Turn an array into Object with property key/value”

  1. I do believe all of the concepts you’ve presented for your post.

    They’re really convincing and will definitely work. Nonetheless, the
    posts are very brief for beginners. Could you please extend them a bit from next
    time? Thank you for the post.

    Reply
  2. Attractive element of content. I just stumbled upon your blog and in accession capital to claim that I acquire
    actually loved account your blog posts. Any way I’ll be subscribing for your augment or even I fulfillment you get entry to persistently
    quickly.

    Reply
  3. I know this if off topic but I’m looking into starting my own weblog and was wondering what
    all is required to get setup? I’m assuming having a blog
    like yours would cost a pretty penny? I’m not very web
    savvy so I’m not 100% certain. Any tips or advice would be greatly appreciated.
    Kudos

    Reply
  4. Can I just say what a relief to uncover an individual who really understands what they are discussing on the
    web. You actually know how to bring a problem to light and
    make it important. More people have to read this and understand
    this side of your story. I can’t believe you’re not
    more popular since you most certainly possess the gift.

    Reply
  5. Thanks for your personal marvelous posting! I truly enjoyed reading it, you happen to be a great author.
    I will be sure to bookmark your blog and will often come back very
    soon. I want to encourage you to ultimately continue your great job, have a nice
    weekend!

    Reply
  6. Good day! This is kind of off topic but I need some advice from an established blog.

    Is it very difficult to set up your own blog?
    I’m not very techincal but I can figure things out pretty quick.
    I’m thinking about making my own but I’m not sure where to start.
    Do you have any tips or suggestions? Thanks

    Reply
  7. Hi there, I found your web site by way of Google while looking
    for a related subject, your website came up, it appears to
    be like good. I have bookmarked it in my google bookmarks.

    Hello there, just became alert to your blog thru Google, and
    located that it’s really informative. I am gonna be careful for brussels.

    I’ll appreciate if you continue this in future. Many people can be benefited out of your writing.
    Cheers!

    Reply
  8. excellent put up, very informative. I ponder why the opposite specialists of this sector don’t notice
    this. You should continue your writing. I am sure, you have a huge readers’ base already!

    Reply
  9. With havin so much content do you ever run into any problems of plagorism or copyright infringement?
    My site has a lot of unique content I’ve either authored
    myself or outsourced but it looks like a lot of it is popping it up all over the web without
    my authorization. Do you know any methods to help stop content from being stolen? I’d genuinely
    appreciate it.

    Reply
  10. Hi! Someone in my Facebook group shared this website with us so I came to take a look.
    I’m definitely enjoying the information. I’m bookmarking and will be
    tweeting this to my followers! Exceptional blog and fantastic design.

    Reply
  11. Great beat ! I wish to apprentice at the same time as you amend your site, how can i subscribe for a blog site?

    The account aided me a applicable deal. I had been a little bit acquainted of this your broadcast provided brilliant transparent concept

    Reply
  12. Greetings! Quick question that’s entirely off topic.
    Do you know how to make your site mobile friendly? My weblog looks weird when viewing from my iphone4.
    I’m trying to find a template or plugin that
    might be able to resolve this issue. If you have any suggestions,
    please share. Appreciate it!

    Reply
  13. Just wish to say your article is as astounding. The clarity to your put up
    is just spectacular and that i could suppose you are an expert in this subject.
    Well along with your permission allow me to clutch your RSS feed to keep
    up to date with approaching post. Thanks a million and please carry on the rewarding work.

    Reply
  14. Hi there! I understand this is sort of off-topic
    however I needed to ask. Does running a well-established website
    like yours take a lot of work? I’m completely new to blogging however
    I do write in my diary every day. I’d like to start a blog so I will be able to
    share my personal experience and views online. Please let me know if you have any kind of ideas or tips for brand new
    aspiring blog owners. Appreciate it!

    Reply
  15. Fantastic blog! Do you have any helpful hints for aspiring writers?
    I’m planning to start my own website soon but I’m a little
    lost on everything. Would you advise starting with a
    free platform like WordPress or go for a paid option? There are so
    many options out there that I’m completely confused ..
    Any suggestions? Thank you!

    Reply
  16. Just want to say your article is as amazing.
    The clarity to your put up is simply cool and
    that i could suppose you are a professional on this subject.

    Fine together with your permission allow me to take hold of your feed to stay updated with coming near near post.

    Thanks a million and please keep up the gratifying work.

    Reply
  17. Definitely believe that that you stated. Your favorite justification appeared to be on the web
    the easiest factor to have in mind of. I say to you, I definitely get annoyed
    while folks consider issues that they plainly do not realize about.
    You managed to hit the nail upon the highest and also
    outlined out the entire thing with no need side-effects , people can take
    a signal. Will probably be again to get more.

    Thanks

    Reply
  18. I like the valuable info you provide for your articles.
    I’ll bookmark your weblog and test once more right here
    regularly. I am fairly certain I will learn plenty of new stuff proper right here!
    Best of luck for the following!

    Reply
  19. Hello there! This is kind of off topic but I need some
    help from an established blog. Is it difficult to set up your own blog?

    I’m not very techincal but I can figure things out pretty quick.
    I’m thinking about creating my own but I’m not sure
    where to begin. Do you have any ideas or suggestions? Thank
    you

    Reply
  20. Attractive element of content. I simply stumbled upon your site and in accession capital to claim that I acquire in fact loved account your weblog posts.
    Any way I will be subscribing in your feeds or even I fulfillment you
    get entry to persistently fast.

    Reply
  21. Fantastic website you have here but I was wondering if you knew of any discussion boards that cover the same topics talked about in this article?
    I’d really like to be a part of online community where I can get feedback from
    other knowledgeable individuals that share the same interest.
    If you have any recommendations, please let me know.
    Thank you!

    Reply
  22. I’d like to thank you for the efforts you’ve put in writing this blog.

    I’m hoping to check out the same high-grade content from you in the future as well.
    In truth, your creative writing abilities has
    motivated me to get my own, personal website now
    😉

    Reply
  23. I was curious if you ever thought of changing the layout of your website?

    Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content so
    people could connect with it better. Youve
    got an awful lot of text for only having one or 2 pictures.
    Maybe you could space it out better?

    Reply
  24. Woah! I’m really loving the template/theme of this site.
    It’s simple, yet effective. A lot of times it’s
    tough to get that “perfect balance” between usability and appearance.

    I must say you’ve done a superb job with this. In addition, the blog
    loads super quick for me on Opera. Outstanding Blog!

    Reply
  25. Howdy, i read your blog occasionally and i own a similar
    one and i was just curious if you get a lot of spam remarks?
    If so how do you protect against it, any plugin or anything you can recommend?
    I get so much lately it’s driving me crazy
    so any support is very much appreciated.

    Reply
  26. Hey there! I know this is kind of off topic but I was wondering which blog platform are you using
    for this site? I’m getting fed up of WordPress because I’ve had problems with hackers and I’m
    looking at alternatives for another platform.
    I would be great if you could point me in the direction of a good platform.

    Reply
  27. Hmm is anyone else experiencing problems with the pictures on this blog loading?

    I’m trying to find out if its a problem on my end or if
    it’s the blog. Any feed-back would be greatly appreciated.

    Reply
  28. Right here is the right webpage for everyone who really wants to understand
    this topic. You know a whole lot its almost hard to argue with you
    (not that I really would want to…HaHa).
    You certainly put a brand new spin on a topic that has been written about for decades.
    Excellent stuff, just great!

    Reply
  29. My partner and I stumbled over here different web address and
    thought I might as well check things out. I like what I see so now i am following you.
    Look forward to looking over your web page for a second time.

    Reply
  30. I really like your blog.. very nice colors & theme.
    Did you make this website yourself or did you hire someone to do it for
    you? Plz reply as I’m looking to create my own blog and would like to
    know where u got this from. kudos

    Reply
  31. I’ve been surfing online more than three hours lately, but I never found any attention-grabbing article like yours.

    It’s pretty value enough for me. Personally, if all website owners and
    bloggers made excellent content as you did, the internet will probably be a lot
    more helpful than ever before.

    Reply
  32. Hi, I think your website might be having browser compatibility issues.
    When I look at your blog site in Opera, it looks fine but when opening in Internet Explorer, it has some overlapping.
    I just wanted to give you a quick heads up! Other then that,
    wonderful blog!

    Reply
  33. Hi there! This article couldn’t be written much better!
    Going through this post reminds me of my previous roommate!

    He continually kept talking about this. I am going to send
    this information to him. Fairly certain he’s going to have a good read.
    Thanks for sharing!

    Reply
  34. Heya exceptional blog! Does running a blog similar to this take a great
    deal of work? I’ve absolutely no understanding of coding however I had been hoping to start my
    own blog soon. Anyway, if you have any recommendations or techniques for new blog owners please share.
    I know this is off topic but I just needed
    to ask. Many thanks!

    Reply
  35. I have been browsing on-line greater than three hours lately,
    but I never found any interesting article like yours.
    It’s lovely worth enough for me. In my view, if all site owners and bloggers
    made good content as you probably did, the web can be much more useful
    than ever before.

    Reply

Leave a Comment