0

I hava a string like this "sum 123,645,423,123,432";

How can i convert this string to be like this:

{
  “sum”: [ 123,645,423,123,432 ]
}

I try it like this:

var arr = "sum 123,645,423,123,432";
var c = arr.split(',');
console.log(c);
VM3060:1 (5) ["sum 123", "645", "423", "123", "432"]

Thanks!

3
  • 3
    What did you try to convert it like that? Commented Jun 15, 2020 at 9:08
  • 1
    Please read idownvotedbecau.se/noattempt Commented Jun 15, 2020 at 9:10
  • Posted an answer below based on your try, hope it will help you somehow Commented Jun 15, 2020 at 9:25

4 Answers 4

5

First, i .split() the string by whitespace, that returns me an array like this ["sum" , "123,645,423,123,432"]

Instead of writing var name = str.split(" ")[0] and var arrString = str.split(" ")[1] i used an destructuring assignment

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

Next step is to split the arrString up by , and then .map() over each element and convert it to an number with Number().

Finally i assign an object to result with a dynamic key [name] and set arr to the dynamic property.

var str = "sum 123,645,423,123,432";

var [name,arrString] = str.split(" ");
var arr = arrString.split(",").map(Number);

let result = {
   [name]: arr
}

console.log(result);

//reverse

var [keyname] = Object.keys(result);
var strngArr = arr.join(",");

var str = `${keyname} ${strngArr}`

console.log(str);

Sign up to request clarification or add additional context in comments.

7 Comments

work but the value in the array must be number not string
@lfaruki glad to see you removed the typings. You can improve your answer by pointing out to some destructuring post to guide others to "what those square brackets mean". Eventually add a short explanation above, or as comments in your code with what you've done there, so it will be easier for beginners to understand.
@BaruchMashasha try var result = arr.join(",")
@BaruchMashasha well look at my edit, i have added it at the bottom
@Ifaruki Amazing, and the explanation was excellent, thank you very much !!!
|
2

const str = "sum 123,645,423,123,432";

const splittedString = str.split(" ");
const key = splittedString[0];
const values = splittedString[1].split(",").map(Number);

const myObject = {
  [key]: [...values]
};

console.log(myObject);

3 Comments

work but the value in the array must be number not string
It works, but it's Typescript, not Javascript. Uncaught SyntaxError: Missing initializer in const declaration
@JeremyThille yeah sure this is a typed version sorry. I removed the types
1

There are many ways to dot that,one way to do it using String.prototype.split()

let str = "sum 123,645,423,123,432";
let split_str = str.split(' ');
let expected = {};
expected[split_str[0]] = split_str[1].split(',');
console.log(expected);

2 Comments

work but the value in the array must be number not string
@BaruchMashasha have a look now :)
1

This solution is equivalent to @Yohan Dahmani with the use of destructuring array for more legible code.

const str = "sum 123,645,423,123,432";

const [key,numbersStr] = str.split(' ');

const numbersArr = numbersStr.split(',').map(n => parseInt(n, 10));

const result = {[key]: numbersArr};

console.log(result);

1 Comment

Done. With that concept given I think is enough to find an extended explanation over Internet. I found that basic and not totally related with the question, that's why I didn't explain, maybe it's more interesting to explain what [key] means indeed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.