Removing object by matching string in it [duplicate] - javascript

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

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

Best way to sort array based of different sets of data? [duplicate]

This question already has answers here:
Javascript sort array of objects using array of priority
(2 answers)
Closed 1 year ago.
var array = [{value:"13",type:"Fruit"},{value:"61",type:"Animal"},
{value:"19",type:"Fruit"},{value:"71",type:"Animal"},
{value:"12",type:"Fruit"},{value:"15",type:"Fruit"},
{value:"11",type:"Plant"},{value:"10",type:"Fruit"},
{value:"16",type:"Plant"}]
What is the best/optimized way to sort this array such that I get all elements of type Fruit first and then the elements of type Animal (by picking elements from end of array).
expectedOutput = [{value:"10",type:"Fruit"},
{value:"15",type:"Fruit"},{value:"12",type:"Fruits"},
{value:"19",type:"Fruit"},{value:"13",type:"Fruit"},
{value:"71",type:"Animal"},{value:"61",type:"Animal"},
{value:"16",type:"Plant"},{value:"11",type:"Plant"}]
Note:- I don't need to sort it Alphabetically. It must be sort depending upon the specific type.
You can store the order of the type properties in an array, then subtract the index of the type property when sorting to determine precedence:
const order = ["Fruits", "Animal"]
var array = [{value:"13",type:"Fruit"},{value:"61",type:"Animal"},
{value:"19",type:"Fruit"},{value:"71",type:"Animal"},
{value:"12",type:"Fruit"},{value:"15",type:"Fruit"}]
const sorted = array.sort((a,b) => order.indexOf(a.type) - order.indexOf(b.type))
console.log(sorted)

How can I remove all duplicated elements in an array (including first occurrence) [duplicate]

This question already has answers here:
Get all unique values in a JavaScript array (remove duplicates)
(91 answers)
Merge sorted arrays and remove duplicates javascript
(5 answers)
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
I was studying JS in CodeWars and I didn't find a method to remove all duplicated elements in an array. I need to do exactly this:
a = [1,2,2,2,3,4,5,6,6,7]
b = [1,2,7,8,9]
Return a unique array = [3,4,5,8,9]
delete all the duplicated items, including the first occurrence
How can I do this? I already use for, if, forEach, but no success.
You may simply
count the occurences of each element (to preserve the original element types, you may apply Array.prototype.reduce() together with Map against merged array)
then, filter out those that are seen more than once:
const a = [1,2,2,2,3,4,5,6,6,7],
b = [1,2,7,8,9],
uniques = [
...[...a, ...b]
.reduce((acc,item) =>
(acc.set(item, (acc.get(item)||0)+1), acc), new Map)
.entries()
].reduce((acc, [key, value]) =>
(value === 1 && acc.push(key), acc), [])
console.log(uniques)
.as-console-wrapper {min-height:100%}

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

sort javascript array by each object attribute [duplicate]

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...]]

Categories