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.
TypeScript 3.4+
TypeScript version 3.4 has introduced so-called **const contexts**, which is a way to declare a tuple type as immutable and get the narrow literal type directly (without the need to call a function like shown below in the 3.0 solution).With this new syntax, we get this nice concise solution:
const furniture = ['chair', 'table', 'lamp'] as const;
type Furniture = typeof furniture[number];
More about the new const contexts is found in this PR as well as in the release notes.
TypeScript 3.0+
With the use of generic rest parameters, there is a way to correctly infer string[] as a literal tuple type and then get the union type of the literals.
It goes like this:
const tuple = <T extends string[]>(...args: T) => args;
const furniture = tuple('chair', 'table', 'lamp');
type Furniture = typeof furniture[number];
More about generic rest parameters
This answer is out of date; see @ggradnig's answer.
The best available workaround:
const furnitureObj = { chair: 1, table: 1, lamp: 1 };
type Furniture = keyof typeof furnitureObj;
const furniture = Object.keys(furnitureObj) as Furniture[];
Ideally we could do this:
const furniture = ['chair', 'table', 'lamp'];
type Furniture = typeof furniture[number];
Unfortunately, today furniture is inferred as string[], which means Furniture is now also a string.
We can enforce the typing as a literal with a manual annotation, but it brings back the duplication:
const furniture = ["chair", "table", "lamp"] as ["chair", "table", "lamp"];
type Furniture = typeof furniture[number];
TypeScript issue #10195 tracks the ability to hint to TypeScript that the list should be inferred as a static tuple and not string[], so maybe in the future this will be possible.