You dont need lodash, you can do with plain JS using map
DEMO
const tags = [{
"display": "tag1",
"value": "tag1"
}, {
"display": "tag2",
"value": "tag2"
}]
var result = tags.map(a => a.display);
console.log(result);
Answer from Sajeetharan on Stack Overflowjavascript - Convert JSON array of objects to string array using Typescript - Stack Overflow
[Question] Converting array of objects into a single object
How do I convert a javascript object array to a string array of the object attribute I want? - Stack Overflow
Convert array of key:value pair objects to string
Basically what I'm trying to achieve is to convert an array like this:
const params = [
{ name: "name", type: "STRING" },
{ name: "age", type: "INTEGER" },
{ name: "admin", type: "BOOLEAN" }
]; into a type like this:
{ name: string, age: number, admin: boolean }I've figured a way to convert individual objects into their proper representation like this:
type ParamType = "STRING" | "INTEGER" | "BOOLEAN";
type GetParamType<T extends ParamType> =
T extends "STRING" ? string :
T extends "INTEGER" ? number :
T extends "BOOLEAN" ? boolean :
never;
interface Param {
name: string;
type: ParamType
}
type GetTypeFromParam<P extends { type: ParamType }> =
P extends { type: ParamType } ? GetParamType<P["type"]> : never;
type GetNameFromParam<P extends { name: string }> =
P extends { name: string } ? P["name"] : never;
type FormatParam<P extends Param> = Record<GetNameFromParam<P>, GetTypeFromParam<P>>
type ParamTest = FormatParam<{ name: "test", type: "INTEGER" }> // { test: number }But I can't figure out how to "concat" an array into a single object. Is there something I can do to achieve this or is it simply impossible to do?
Thanks in advance.
If your array of objects is items, you can do:
var items = [{
id: 1,
name: 'john'
}, {
id: 2,
name: 'jane'
}, {
id: 2000,
name: 'zack'
}];
var names = items.map(function(item) {
return item['name'];
});
console.log(names);
console.log(items);
Documentation: map()
Use the map() function native on JavaScript arrays:
var yourArray = [ {
'id':1,
'name':'john'
},{
'id':2,
'name':'jane'
}........,{
'id':2000,
'name':'zack'
}];
var newArray = yourArray.map( function( el ){
return el.name;
});
I'm getting an array like this [ {name: 'a', value: 'b'}, {name: 'c', value: 'd'}, {name: 'e', value: 'f'} ]. I need to convert this array to a string, like 'a=b/c=d/e=f' to send to an API. How can I do this? I have tried all sorts of Array map tricks and Object.values or keys stuff to no avail.