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 OverflowHow 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
[Question] Converting array of objects into a single object
How to Type an array that contains objects and strings
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.
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.
Hello everyone.
I'm having a hard time getting the linter to accept the typing of an array of mostly objects that can contain a string.
exampleArray: [{id: 'abc', name: 'test2'}, {id: 'abc', name: 'test2'}, 'ALL'];I have typed the array accordingly:
export interface ITest {
id?: string;
name?: string;
}
export type TestOption = ITest | string;I can't get the tests to pass. Examples of the test outputs are found below:
#``1:
error TS2339: Property 'displayValue' does not exist on type 'string | ITest'.
Property 'name' does not exist on type 'string'.
const labelId = `${multiple ? 'checkbox-' : ''}list-label-${option?.name || ''}`;#2:
src/components/FilterSelect/index.tsx:274:142 - error TS2339: Property 'displayValue' does not exist on type 'string | ITest'.
Property 'name' does not exist on type 'string'.
274 <ListItemText classes={{ primary: classes.listItemText, root: classes.listItemTextRoot }} id={labelId} primary={option.name} />If anyone has any advice or documentation that will help please send it my way! ๐