convert a key value object to a single array [duplicate] - javascript

This question already has answers here:
How to convert key-value pair object into an array of values in ES6?
(5 answers)
Closed 1 year ago.
I need to convert a object
{score: 77, id: 166}
to an array,
[77,166]
I tried,
Object.keys(obj).map((key) => [obj[key]]);
but seems like, it returns as 2 arrays.
[[77][166]]

You just had an extra pair of square brackets in your code
const obj = {score: 77, id: 166};
const result = Object.keys(obj).map((key) => obj[key]);
console.log(result)

You can use also use Object.values(obj) to achieve this result
const obj = {
score: 77,
id: 166
}
const result = Object.values(obj)
console.log(result);
this will return you an array of values

Related

Sort by values but alphabetical order when values are same in Javascript [duplicate]

This question already has answers here:
How can I sort a javascript array of objects numerically and then alphabetically? [duplicate]
(5 answers)
Closed last month.
If I have the object,
const obj = { Peter: 3, Jeremy: 2, Chris: 1, Adam: 2 };
I want to compare object values, and sort them in a numerical order.
Therefore, I tried
let answer = Object.keys(obj);
answer.sort((a,b) => {
return obj[b] - obj[a];
})
The output is ['Peter', 'Jeremy', 'Adam', 'Chris'].
I want to sort Jeremy and Adam in Alphabetical order as they have same values.
The output I want is ['Peter', 'Adam', 'Jeremy', 'Chris']
How do I approach to this answer?
you can use a condition for case where values are same to sort based on alphabetical order of keys
const obj = { Peter: 3, Jeremy: 2, Chris: 1, Adam: 2 };
let answer = Object.keys(obj);
answer.sort((a,b) => {
if (obj[b] == obj[a]) return a.localeCompare(b)
return obj[b] - obj[a];
})
console.log(answer)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare
const obj = { Peter: 3, Jeremy: 2, Chris: 1, Adam: 2 };
const result = Object.entries(obj) // get array of [key, value]
// sort by value descending, then alphabetically by key
.sort(([a,b],[c,d])=>d-b || a.localeCompare(c))
.map(([i])=>i) // extract just the key
console.log(result)

How to make an array of Objects into an Array of String [duplicate]

This question already has answers here:
Converting Json Object array Values to a single array
(5 answers)
Closed 2 years ago.
If I have an array such as:
let arr = [{subject: "BSE", courseCode: "1010"},{subject: "STA", courseCode: "2020"}];
Is it possible to make the array only containing the value pairs of the object such as:
let result = ["BSE","1010","STA","2020"];
Using Object.prototype.values, you can generate only values from an object.
let arr = [{subject: "BSE", courseCode: "1010"},{subject: "STA", courseCode: "2020"}];
const output = arr.flatMap((item) => Object.values(item));
console.log(output);

array.map does not change array values [duplicate]

This question already has an answer here:
Javascript Array.prototype.filter() not working
(1 answer)
Closed 3 years ago.
Why doesn't array.map work?
My code is:
let myArray = [000,111,222,333,444,555,666,777,888,999];
myArray.map((value) => {
return = 1000 - value;
});
console.log(myArray);
The result is:
[0, 111, 222, 333, 444, 555, 666, 777, 888, 999]
Calling map returns a new array. It doesn't modify the old one in place. Do this instead:
let myArray = [000,111,222,333,444,555,666,777,888,999];
let myNewArray = myArray.map((value) => {
return 1000 - value;
});
console.log(myNewArray);

How to convert an Array to Array of Object in Javascript [duplicate]

This question already has answers here:
Javascript string array to object [duplicate]
(4 answers)
JS : Convert Array of Strings to Array of Objects
(1 answer)
Convert array of strings into an array of objects
(6 answers)
Closed 3 years ago.
I want to convert an Array like:
[ 'John', 'Jane' ]
into an array of object pairs like this:
[{'name': 'John'}, {'name':'Jane'}]
Please help me to do so..
Try the "map" function from the array:
const output = [ 'John', 'Jane' ].map(name => ({name}));
console.log(output);
You can use the instance method .map() on a JS list Object as follows :
let list = ['John', 'Jane']
list = list.map(x => {
return({name: x});
});
console.log(list);

filter Object of arrays JavaScript [duplicate]

This question already has answers here:
How can I remove a specific item from an array in JavaScript?
(142 answers)
How do I loop through or enumerate a JavaScript object?
(48 answers)
Closed 3 years ago.
Let's say I have this Object of arrays:
foo = {morning: [1,2,3,4,5], afternoon: [1,2,3,4,7]}
I want to write a function that returns this object but remove a particular value.
ex: I want to remove the number 3 in afternoon.
The function would return {morning: [1,2,3,4,5], afternoon: [1,2,4,7]}
myFunction = (partsOfDay, number) => {
// do something
// returns the object of arrays but without the specified value
}
How can I do that ?
You can do this without changing the source object using Array.reduce() and Object.entries().
The properties of the returned object will still point to the source object but the filtered array properties will be copied with Array.filter().
const foo = { morning: [1,2,3,4,5], afternoon: [1,2,3,4,7] };
const myFilter = (obj, prop, value) => Object.entries(obj).reduce((acc, [key, val]) => {
acc[key] = key === prop && Array.isArray(val) ? val.filter(x => x !== value) : val;
return acc;
}, {});
console.log(myFilter(foo, 'afternoon', 3));
console.log(myFilter(foo, 'morning', 3));
console.log(foo);
There are many ways, something like this
var partsOfDay = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
myFunction = (partsOfDay, number) => {
var filtered = partsOfDay.filter(function(value, index, arr){
return value != number;
});
}
Refer for more here

Categories

Resources