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

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

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

convert array of hashes to array of values in javascript [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 2 years ago.
I am an array of hashes and I want to convert it to array of values something like this
array of hashes [{text: "James"},{text: "developer"}]
convert this to
array of values ["James", "Developer"]
Please help me achieve this in javascript.
Using Object.values, you can get the values of each object.
const input = [{text: "James"},{text: "developer"}];
const output = input.flatMap((item) => Object.values(item));
console.log(output);

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

Sort an arary of objects by date [duplicate]

This question already has answers here:
Sort array of objects by single key with date value
(19 answers)
Closed 8 years ago.
I got an array of objects which I want to sort by date. I've got the date formatted like this with a property - created_at: "2014-09-26 19:27:43".
If any one could point me in the right direction, that would be great.
Use Date objects for comparison and Array.prototype.sort() to sort.
var array = ["2014-09-26 19:27:43","2014-09-26 19:27:42","2014-09-23 19:27:43"];
var sortedArray = array.sort(function(a,b){
return new Date(a) - new Date(b);
});
document.write(sortedArray);

Categories