-1

I have the following array

 [ { "id": 1, "name": "Test" }, { "id": 2, "name": "Test2" }

How can I convert that to

 [ { "id": '1', "name": "Test" }, { "id": '2', "name": "Test2" }
Petran
  • 7,677
  • 22
  • 65
  • 104
  • using javascript - what have you tried?, perhaps "toString" is a start – Bravo Nov 06 '18 at 11:23
  • Ideal use case of `Array#map` – Rayon Nov 06 '18 at 11:24
  • map, for a new array, forEach for change in place – Bravo Nov 06 '18 at 11:25
  • Please, show what have you tried so far, then provide a [MCVE : Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). If people around can easily read and understand what you mean, or what the problem is, they'll be more likely willing to help :) – Dwhitz Nov 06 '18 at 11:25
  • Possible duplicate of [What's the best way to convert a number to a string in JavaScript?](https://stackoverflow.com/questions/5765398/whats-the-best-way-to-convert-a-number-to-a-string-in-javascript) – cнŝdk Nov 06 '18 at 11:25

1 Answers1

2

For all the objects in an array, you want to convert a property type from number to string. You can use Array.forEach to achieve the same.

let arr = [ { "id": 1, "name": "Test" }, { "id": 2, "name": "Test2" }];
arr.forEach(v => v.id += '');
console.log(arr);
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59