1

I have an string from backend source, of format:

numbers: "1123763449056714753, 1123764853364097024, 1123770213739241473..."

I want to convert it into key-value pair something form so that I can map over these ids for my individual item div:

["numbers": [ 
              {"id":1123763449056714753 }, 
              {"id":1123764853364097024 },
              {"id":1123770213739241473 },
             ....
            ]
]

or

[                 {"id":1123763449056714753 }, 
                  {"id":1123764853364097024 },
                  {"id":1123770213739241473 },
                 ....

    ]

I tried using forEach()/ map() but gets error as undefined. Also I am not sure whether it is a collection of string or array of strings Please help

5
  • 2
    considering the whole variable is string. Use let obj = JSON.parse('{' +str+'}'); obj.numbers = obj.numbers.split(',').map(id => ({id})) Commented May 2, 2019 at 12:47
  • Is the input a comma separated string or an array of strings as you mentioned in the title? Also, your output is invalid. Please post a valid input and expected outputs to create a minimal reproducible example Commented May 2, 2019 at 12:48
  • 1
    object.numbers = object.numbers.split(',').map(i => { id: +i }. The question is quite unclear and doesn't hold the minimal amount of informations needed to verify the example. Please clarify it and add what you've tried. Commented May 2, 2019 at 12:48
  • Also, it's good to post the code that isn't quite working. That helps understand the original intent and give an answer that is more useful. (I agree with the two previous comments). Commented May 2, 2019 at 12:49
  • 1
    In the output, idis of type number. All these numbers are bigger than Number.MAX_SAFE_INTEGER. So, when string converted to number, they won't be the same numbers anymore Commented May 2, 2019 at 12:54

6 Answers 6

3

You could split the string and map the objects by keeping the strings (who are larger than allowed integer values).

var object = { numbers: "1123763449056714753,1123764853364097024,1123770213739241473,1123771975162368003" },
    numbers = object.numbers.split(',').map(id => ({ id }));

console.log(numbers);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

3 Comments

Isn't the id meant to be a numeric value (looking at his example)? Edit: NVM, you've fixed that already :P
FYI, the numbers are bigger than Number.MAX_SAFE_INTEGER
@adiga nice catch, didn't notice that! So, technically, the "sample" output is somewhat invalid.
2

You can split the object then map it and return the structure you want :

const obj = {
  "numbers": "1123763449056714753,1123764853364097024,1123770213739241473,1123771975162368003..."
}


const res = {
  numbers: [
    obj.numbers.split(",").map(el => ({
      id: el
    }))
  ]
}

console.log(res);

Comments

1

var arr = ["1123763449056714753","1123764853364097024","1123770213739241473","1123771975162368003"];
var newArr = [];
for (var i = 0; i < arr.length; i++) {
    var obj = {};
    obj.id = arr[i];
    newArr.push(obj);
}
console.log(newArr)

Comments

1

You can do something like this.

var data = {"numbers":"1123763449056714753,1123764853364097024,1123770213739241473,1123771975162368003"};

data.numbers = data.numbers.split(',').reduce((res, id) => [...res, {id}], []);

Comments

1

One thing you could do is get the numbers as an array:

var obj = {
  "numbers": "1123763449056714753,1123764853364097024,1123770213739241473,1123771975162368003"
};

var result = obj.numbers.split(',').reduce((acc, num) => {
  acc.push({ id: num });
  return acc;
}, []);

console.log(result)

or as @briosheje mentioned you could also use map:

var result = obj.numbers.split(',').map(num => ({id: num}));

The result would look like this:

[{"id":"1123763449056714753"},{"id":"1123764853364097024"},{"id":"1123770213739241473"},{"id":"1123771975162368003"}]

Comments

0

As per your data, you can just split the numbers string using "," and loop through it to perform the transform, like

var data = {"numbers":"1123763449056714753,1123764853364097024,1123770213739241473,1123771975162368003"};
var numbers_arr = data.numbers.split(",");
var result = {
  "numbers":[]
};
for(var i=0;i<numbers_arr.length;i++){
 var tempObj = {
    "id":parseInt(numbers_arr[i])
  };
  result.numbers.push(tempObj)
}

console.log(JSON.stringify(result));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.