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

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

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'}]

How to convert key-value pairs to object using JavaScript? [duplicate]

This question already has answers here:
Convert form data to JavaScript object with jQuery
(58 answers)
Closed 2 years ago.
I want to form values to object/array like var date = {"first_name": "X", "last_name": "Y"};
I am using data = $(form).serialize() but it is produce first_name:X,last_name:Y
If I am using data = $(form).serializeArray() then it is taking other values too like select all options, so it is wrong.
So how to convert above key-value pairs to object using JavaScript?
serialize() is used to encode form data into URLs. Instead, use the FormData object.
const formdata = new FormData($("#formId"));

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

Javascript Take JSON string convert to array [duplicate]

This question already has answers here:
How can I convert a comma-separated string to an array?
(19 answers)
Closed 3 years ago.
*****No JQUERY*****
I have a string passed into my Javascript that looks like below. I want to convert it into an array.
I have
{"test":"1,180,35"}
I want
an array where index 0 = 1, index 1 = 180, index 2 = 35.
How would I achieve this?
Parse the string, pull out the property value for property test, split it on ,.
var input = '{"test":"1,180,35"}'
var jsObj = JSON.parse(input);
var arr = jsObj.test.split(",");
console.log(arr);
use JSON.parse() to convert a string into a json object.
But, you are looking to parse a series of numbers into an array, so what you really want is split(",")
Use the JSON object
let arr = JSON.parse('{"test":"1,180,35"}').test.split(',');
For example:
var yourData = `{"test":"1,180,35"}`
JSON.parse(yourData).split(',')

Categories