javascriptarraysjson

convert JSON array of key value pairs to JSON Array of strings in Javascript


so i have a JSON Object [{"key1":"val1","key2":"val2"},{"key1":"val1","key2":"val2"}]

and i essentially need to remove the keys so the output looks like [["val1","val2"],["val1","val2"]] in Javascript.

short of iterating through the array and then iterating through all the properties and mapping to a new JSON object is there any way i can remove the keys from the object, and turn the values in to a list in an array?

please no string splicing/ regex.

Thanks.


Solution

  • Using ES2015 (ES6)

    const arr = [{"key1":"val1","key2":"val2"},{"key1":"val1","key2":"val2"}]
    var newArr=arr.map(o => Object.values(o));
    
    console.log(newArr);

    See