This question already has answers here:
Javascript string array to object [duplicate]
(4 answers)
JS : Convert Array of Strings to Array of Objects
(1 answer)
Convert array of strings into an array of objects
(6 answers)
Closed 3 years ago.
I want to convert an Array like:
[ 'John', 'Jane' ]
into an array of object pairs like this:
[{'name': 'John'}, {'name':'Jane'}]
Please help me to do so..
Try the "map" function from the array:
const output = [ 'John', 'Jane' ].map(name => ({name}));
console.log(output);
You can use the instance method .map() on a JS list Object as follows :
let list = ['John', 'Jane']
list = list.map(x => {
return({name: x});
});
console.log(list);
Related
This question already has answers here:
How to convert key-value pair object into an array of values in ES6?
(5 answers)
Closed 1 year ago.
I need to convert a object
{score: 77, id: 166}
to an array,
[77,166]
I tried,
Object.keys(obj).map((key) => [obj[key]]);
but seems like, it returns as 2 arrays.
[[77][166]]
You just had an extra pair of square brackets in your code
const obj = {score: 77, id: 166};
const result = Object.keys(obj).map((key) => obj[key]);
console.log(result)
You can use also use Object.values(obj) to achieve this result
const obj = {
score: 77,
id: 166
}
const result = Object.values(obj)
console.log(result);
this will return you an array of values
This question already has answers here:
Converting Json Object array Values to a single array
(5 answers)
Closed 2 years ago.
If I have an array such as:
let arr = [{subject: "BSE", courseCode: "1010"},{subject: "STA", courseCode: "2020"}];
Is it possible to make the array only containing the value pairs of the object such as:
let result = ["BSE","1010","STA","2020"];
Using Object.prototype.values, you can generate only values from an object.
let arr = [{subject: "BSE", courseCode: "1010"},{subject: "STA", courseCode: "2020"}];
const output = arr.flatMap((item) => Object.values(item));
console.log(output);
This question already has answers here:
Sort an array of object by a property (with custom order, not alphabetically)
(7 answers)
Sort array of objects by string property value
(57 answers)
Closed 2 years ago.
const arr = [
{Id:"3",name: "ADMIN"},
{Id:"1",name: "SECURITY"},
{Id:"2",name: "INFORMATION_REPORTING"},
{Id: "23",name: "PAYMENTS_SERVICES"},
{Id: "344",name: "PAYMENT_HUB"},
{Id: "31",name: "RTP"},
{Id: "43",name: "PAYMENTS"},
{Id: "34",name: "GPI_ALERTS"},
{Id: "65",name: "ADMINISTRATION"}
]
I have the arr which has the values as describing here.And I want to reorder the arr using the key name as below, Order to be shown.
ADMIN
ADMINISTRATION
PAYMENTS
RTP
PAYMENTS_SERVICES
INFORMATION_REPORTING
PAYMENT_HUB
SECURITY
GPI_ALERTS
So I want the arr in this order shown above based on name key.
You may use Array.prototype.sort() and compare arr items based on their position (Array.prototype.indexOf()) within orderList
const arr = [{Id:"3",name:"ADMIN"},{Id:"1",name:"SECURITY"},{Id:"2",name:"INFORMATION_REPORTING"},{Id:"23",name:"PAYMENTS_SERVICES"},{Id:"344",name:"PAYMENT_HUB"},{Id:"31",name:"RTP"},{Id:"43",name:"PAYMENTS"},{Id:"34",name:"GPI_ALERTS"},{Id:"65",name:"ADMINISTRATION"}],
orderList = ['ADMIN','ADMINISTRATION','PAYMENTS','RTP','PAYMENTS_SERVICES','INFORMATION_REPORTING','PAYMENT_HUB','SECURITY','GPI_ALERTS'],
result = arr.sort(({name:nameA},{name:nameB}) =>
!orderList.includes(nameA) ?
1 :
!orderList.includes(nameB) ?
-1 :
orderList.indexOf(nameA) - orderList.indexOf(nameB)
)
console.log(result)
.as-console-wrapper{min-height:100%;}
This question already has answers here:
How do I check if an array includes a value in JavaScript?
(60 answers)
Closed 3 years ago.
There is a string variable that has a path like /app/something/xx/4/profile
besides there is an array of string like **
const arr=[
{name:'xx',etc...},
{name:'yy',etc...},
{name:'zz',etc...}
]
I want to find the first index of array that the string variable has the name in simplest way.
Use Array.findIndex which :
returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test.
const str = "/app/something/xx/4/profile";
const arr = [{ name: "xx" }, { name: "yy" }, { name: "zz" }];
const index = arr.findIndex(e => str.includes(e.name));
console.log({ index });
This question already has answers here:
Merge/flatten an array of arrays
(84 answers)
Closed 3 years ago.
I have an array containing a further 2 arrays of objects and I wanted to turn it into a single array of objects. I have posted the current code and result I want
I have tried the concat method but possibly implementing wrong?
var code = [
[{Adults:"1", Price: "14.50"}],
[{Adults:"1", Price: "20.50"}]
]
var result = [
{Adults: "1", Price: "14.50" },
{Adults: "1", Price: "20.50"}
]
You can use flat
var code = [
[{Adults:"1", Price: "14.50"}],
[{Adults:"1", Price: "20.50"}]
]
let op = code.flat()
console.log(op)