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);
Related
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);
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
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...]]
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
This question already has answers here:
How do I write unencoded Json to my View using Razor?
(3 answers)
Closed 7 years ago.
I have ViewBag.Result with string value "[['Garreth','VP'],['Johan','IT'],['Test','QA']]"
I want to convert it as javascript array
var dataset =
[
['Garreth','VP'],
['Johan','IT'],
['Test','QA']
]
Obviously var dataset = '#ViewBag.Result' doesn't work because javascript treat is as string but not array. Any idea how to do this?
Thanks
Just remove the single quotes:
var dataset = #ViewBag.Result