JavaScript How to turn a string onto a map? turn string into array,turn string into int,turn string

I need to turn a string formatted like that:

string = "John:31,Miranda:28"

Onto this;

obj = { "John" => 31, "Miranda" => 28 }

I did this :

const class = new Map();
array = string.split(",");

And obviously I do not know what do with it because after the split I get something like this:

["John:31", "Miranda:28"]

And I don't know how to turn it onto an object (using the ":" as a arrow)... Maybe I don't need to use the array as an intermediary? Any thoughts? Thanks

Answer:1

So split on the commas, loop over it and split on the colon, and build the object.

var myString = "John:31,Miranda:28"
var myObj = myString.split(',').reduce(function (obj, part) {
  var pieces = part.split(':')
  obj[pieces[0]] = pieces[1]
  return obj
}, {})
Answer:2

You could try something like this:

const data = "John:31,Miranda:28"

const splitData = data.split(',')

const result = splitData.reduce((newObject, item) => {
  const [name, age] = item.split(':')
  return {
    ...newObject,
    [name]: parseInt(age)
  }
}, {})

console.log(result)
Answer:3

I am fetching data from the server and getting the data successfully as you can see in the screenshot, I also checked my code. my code is working properly when I take data manually, but I don't know ...

I am fetching data from the server and getting the data successfully as you can see in the screenshot, I also checked my code. my code is working properly when I take data manually, but I don't know ...

  1. typeerror cannot read property
  2. typeerror cannot read property 'map' of undefined
  3. typeerror cannot read property 'length' of undefined
  4. typeerror cannot read property 'push' of undefined
  5. typeerror cannot read property 'setstate' of undefined
  6. typeerror cannot read property '0' of undefined
  7. typeerror cannot read property 'then' of undefined
  8. typeerror cannot read property 'location' of undefined
  9. typeerror cannot read property 'apply' of undefined
  10. typeerror cannot read property 'requestcontent' of undefined
  11. typeerror cannot read property 'contexttypes' of undefined
  12. typeerror cannot read property 'state' of undefined
  13. typeerror cannot read property 'createelement' of undefined
  14. typeerror cannot read property 'props' of undefined
  15. typeerror cannot read property 'kind' of undefined
  16. typeerror cannot read property 'get' of undefined
  17. typeerror cannot read property 'prototype' of undefined
  18. typeerror cannot read property 'subscribe' of undefined
  19. typeerror cannot read property 'foreach' of undefined
  20. typeerror cannot read property 'name' of undefined

As suggested here, I should use canvas renderer so that images can be displayed in nodes. First of all I create a graphClass: graphClass.g = { nodes: [], edges: [] } Then, I create each node:...

As suggested here, I should use canvas renderer so that images can be displayed in nodes. First of all I create a graphClass: graphClass.g = { nodes: [], edges: [] } Then, I create each node:...

I am working on an Angular project where I want to catch all the encCharge to add them to give a total of all the encCharge in my cf's with a for function. This is my array : produits = [ { ...

I am working on an Angular project where I want to catch all the encCharge to add them to give a total of all the encCharge in my cf's with a for function. This is my array : produits = [ { ...

Result shows "n.split is not a function" unless i include n=n+" " the following code.What does third line mean? function reverse_a_number(n) { n = n + ""; return n.split("").reverse()....

Result shows "n.split is not a function" unless i include n=n+" " the following code.What does third line mean? function reverse_a_number(n) { n = n + ""; return n.split("").reverse()....

  1. write javascript function in html
  2. write javascript function in typescript
  3. write javascript function in js file
  4. write javascript function in angular 6
  5. write javascript function in html tag
  6. write javascript function in ts file
  7. write javascript function in onclick
  8. write javascript function in console
  9. write javascript function inside onclick
  10. write javascript function in code behind c#
  11. write javascript function
  12. write javascript function in php
  13. write javascript function in react
  14. write javascript function inside html
  15. write javascript function with parameter
  16. write javascript function in c#
  17. write javascript function with callback
  18. write javascript function with return value
  19. write javascript function for add(3)(4)
  20. write javascript function in href