Merge array within array [duplicate] - javascript

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)

Related

Is there a way to create an array of objects using the Key value pairs of an object as values [duplicate]

This question already has answers here:
Convert object to an array of objects?
(5 answers)
get Array-Object-String thing from the given Object
(4 answers)
How to convert object A to an array of objects with object A's properties and assign value and label to them [duplicate]
(2 answers)
Closed 17 days ago.
I have an object
const myObject = {rice : 10, wheat: 20, sugar: 3}
I would like to convert it to an array of object:
myArray = [
{"product" : rice, "QTY" : 10},
{"product" : wheat, "QTY" : 20},
{"product" : sugar, "QTY" : 3}
]
Please help.
Thanks.
This is where Object.entries, destructuring and shorthand property names can serve:
const myObject = {rice : 10, wheat: 20, sugar: 3}
const result = Object.entries(myObject).map(([product, qty]) => ({ product, qty }));
console.log(result);

Sort array by values in subarrays [duplicate]

This question already has answers here:
How to sort 2 dimensional array by column value?
(14 answers)
Closed 2 years ago.
I have an array like this:
let pairs = [[2,"test"], [7,"buzz"], [3,"asd"]]
How can i sort the outer array by the first values? (descending)
Pass a compare function to Array.sort that examines the first element in each array, to determine the required order.
> let pairs = [[2,"test"], [7,"buzz"], [3,"asd"]]
> pairs.sort((a, b) => a[0] - b[0]);
[ [ 2, 'test' ], [ 3, 'asd' ], [ 7, 'buzz' ] ]

How to make an array of Objects into an Array of String [duplicate]

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);

Sort array of objects based on the ordered list of values [duplicate]

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%;}

How to convert an Array to Array of Object in Javascript [duplicate]

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);

Categories

Resources