sort javascript array by each object attribute [duplicate] - javascript

This question already has answers here:
Sorting an array of objects by property values
(35 answers)
Closed 6 years ago.
I Have an array named 'treeArray':
Array[78]
0
*function:Object
*menu :
id:1
id_parent:1
name:"Settings"
1
*function:Object
*menu:
id:1
id_parent:1
name:"Settings"
2
*function:Object
*menu:
id:1
id_parent:2
name:"game"
I would like to get two arrays :
The first one would contain Objects which follows :
menu.id=menu.id_parent
and the second one where
menu.id!=menu.id_parent
How can i achieve this with lodash ?
Thank you

use _.partition like
_.partition(treeArray, function(item) {
return item.menu.id === item.menu.id_parent;
})
it returns [[...truthly items...], [...falsy items...]]

Related

How to get value from n-dimensional array - (Javascript) [duplicate]

This question already has answers here:
Flattening multidimensional arrays in javascript
(2 answers)
Merge/flatten an array of arrays
(84 answers)
Closed 20 days ago.
I want to get a String value of multi-dimensional array
and push it into a new Array (That'd be an answer!)
For Example :
var dimStr = [ "First-one", ["Double-one", "Double-two", "Double-three",
["Triple-one", "Triple-two"] ], "First-two" ] ;
And the output should be :
output = ["First-one", "Double-one", "Double-two", "Double-three", "Triple-one",
"Triple-two", "First-two" ] ;
Thank you very much in advance for your kindness.
The output should be a new Array that contains every String value from dimStr
Look at the array method .flat().
output = dimStr.flat(3);

Javascript - Sort does not work properly with string [duplicate]

This question already has answers here:
Sort array of objects by string property value
(57 answers)
How to sort an object array by date property?
(25 answers)
Closed 7 months ago.
I am new to JS and trying to sort an array based on a key of the object (each object is an element in the array). I am not sure why the following code does not sort the array properly. I have checked the doc carefully and the code looks correct to me. I tried to replace the date value with numerical numbers and it worked. Not sure what is wrong with the date string.
var dic=[{key: '2022-05-13'}, {key: '2022-05-06'}]
dic.sort(function (a, b) {
return (a.key - b.key);
})
console.log(dic)
Output
[{key: '2022-05-13'}, {key: '2022-05-06'}]
I thought the output should be
[ {key: '2022-05-06'}, {key: '2022-05-13'}]

Addition of array objects based on date in javascript? [duplicate]

This question already has answers here:
Sum similar keys in an array of objects
(15 answers)
How to merge duplicates in an array of objects and sum a specific property? [duplicate]
(2 answers)
Closed 3 years ago.
I have an object array of the form
var arrayA = [
{value:3000, date:"01/01/2020"},
{value:4000, date:"02/13/2020"},
{value:5500, date:"01/01/2020"},
{value:2300, date:"03/03/2020"},
{value:1200, date:"02/13/2020"},
{value:4000, date:"03/03/2020"}
];
I want to add values that have the same date and come up with a new array as below.
var _new_arrayA = [
{value:8500, date:"01/01/2020"},
{value:5200, date:"02/13/2020"},
{value:6300, date:"03/03/2020"}
];
I do not know where to start, therefore i don't have any implementation code

How can i compare and concat two arrays on ReactJS [duplicate]

This question already has answers here:
remove common elements of two arrays in jquery
(4 answers)
Closed 3 years ago.
Lets say i had two arrays
a.["mark#example.com", "bob#example.com"]
b.["carl#example.com", "mark#example.com", "bob#example.com", "josh#example.com"]
How can i make a third array, using the variables that are present in the second one, but not on the first one?
the desired output should be this:
c.["carl#example.com", "josh#example.com"]
const arrayA = ["mark#example.com", "bob#example.com"]
const arrayB = ["carl#example.com", "mark#example.com", "bob#example.com", "josh#example.com"]
const arrayC = arrayB.filter(x => !arrayA.includes(x))
console.log(arrayC)

Removing object by matching string in it [duplicate]

This question already has answers here:
How to filter object array based on attributes?
(21 answers)
Closed 6 years ago.
I have an array where are objects. Here's one example::
color:"rgb(235, 75, 75)"
id:"6912128558"
img:""
name:"AWP | Dragon Lore (Factory New)"
price:"1852.2"
If I would forEach loop that array and if price >= with total, then it's going to remove the full object. I hope it makes sense.
You can create a new array by filtering the objects you want to keep
var cheapObjects = allObjects.filter(o => o.price < total); // whatever "total" is
or the legacy equivalent
allObjects.filter(function(o) { return o.price < 1000 })
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

Categories