Flatten JavaScript Object [duplicate] - javascript

This question already has answers here:
Converting a JS object to an array using jQuery
(18 answers)
Closed 6 years ago.
On my realtime firebase database I store my products with unique keys, i.e.,
- products
|
- KPYGghMerx_AKU8b4ki
|
- name: "product A"
- price: 1.99
- KPYNtki7UWnh5evYQgjT
|
- name: "product B"
- price: 3.99
I retrieve this data as follows
this.productsRef.on('value', (snapshot) => {
var data = snapshot.val();
}
This returns the object as expected, i.e.,
{
KPYGghMerx_AKU8b4ki: {
name: "product A"
price: 1.99
},
KPYNtki7UWnh5evYQgjT: {
name: "product B"
price: 3.99
}
}
What's the easiest way to map these objects into an array without the unique keys? e.g.
{
name: "product A"
price: 1.99
},
{
name: "product B"
price: 3.99
}

Object.values will extract the values from an object and return it as an array:
Object.values({ k1: v1, k2: v2 }); //[v1, v2]
If you need to polyfill it you can write a simple values function:
function values(obj) {
return Object.keys(obj).map(function (key) {
return obj[key];
});
}

Related

Create Product Sort System using JavaScript [duplicate]

This question already has answers here:
Sorting an array of objects by property values
(35 answers)
Closed 19 days ago.
How would I be able to create a product sort system using JavaScript
I have this Array:
let products = [
{
productName: "Product1",
price: 40
},
{
productName: "Product2",
price: 50
},
{
productName: "Product3",
price: 20
},
{
productName: "Product4",
price: 10
},
{
productName: "Product5",
price: 10
},
{
multiple other products with prices
},
];
I want to sort the products by price, so that the products with the lowest price are shown at the top of the page, and the products with the highest price are shown at the bottom of the page. How would I do this?
You can pass a callback to Array#sort that subtracts the prices.
let products=[{productName:"Product1",price:40},{productName:"Product2",price:50},{productName:"Product3",price:20},{productName:"Product4",price:10},{productName:"Product5",price:10},];
products.sort((a, b) => a.price - b.price);
console.log(products);

How do I group a new array? [duplicate]

This question already has answers here:
Most efficient method to groupby on an array of objects
(58 answers)
How can I group an array of objects by key?
(32 answers)
Group array items using object
(19 answers)
javascript | Object grouping
(16 answers)
Closed 1 year ago.
How do I group a new array? I have data that needs to be grouped in this order,
There are 3 companies in total:
Apple
Samsung
Xiaomi
I receive data about employees who work in these companies, and they are displayed as in pictures (the second list), but it is necessary that, as in the first case, I try to do it like this:
let uniqueGroups;
export const Group = [
{
title: "Apple",
index: 1,
},
{
title: "Samsung",
index: 2,
},
{
title: "Xiaomi",
index: 3,
},
];
const data = [
{_person: 'Alex', _type: 'Apple'},
{_person: 'Marty', _type: 'Xiaomi'},
{_person: 'Gloria', _type: 'Apple'},
{_person: 'Melman', _type: 'Samsung'},
{_person: 'Bob', _type: 'Xiaomi'},
]
// reserveList - My data (_type.name - Company name, _person, the person who works for this company)
const handleRenderGroupItems = () => {
if (data) {
return Group.map(({ index, title }) => {
return data.filter(({ id, _type, _person }) => {
if (_type=== title) {
uniqueGroups = [{ id, type: _type, persons: [_person] }];
}
});
});
}
};

How to fetch particular data from array of objects and store in new array in js (vue js)

I am having this array of object
let detail : [
0: {
Code: "Code 1"
Price: "0.00"
},
1: {
Code: "Code 2"
Price: "9.00"
}
]
I want to store the price in an array(for eg: result) so that I can merge it with another existing array of the object(for eg: alldetail)
result = [
0: {
Price:"0.00"
},
1: {
Price:"9.00"
},
]
Using map() method which creates a new array filled with the results of the provided function executing on every element in the calling array.
So, in your case, you'll return an object with the key Price and the value will be the current object with the value of it's Price property.
let detail = [
{
Code: "Code 1",
Price: "0.00"
},
{
Code: "Code 2",
Price: "9.00"
}
];
let result = detail.map(current => {return {Price: current.Price}});
console.log(result);
Use map to create a new array of objects.
const detail = [
{ Code: "Code 1", Price: "0.00" },
{ Code: "Code 2", Price: "9.00" }
];
const result = detail.map(({ Price }) => ({ Price }));
console.log(result);
Additional documentation
Destructuring assignment.

Javascript how we can search if an id is duplicated in an array of arrays? [duplicate]

This question already has answers here:
Shortest way to find duplicates in array of arrays of objects by property
(1 answer)
Find duplicate object by property and Merge using Javascript or UnderscoreJS
(1 answer)
Closed 2 years ago.
I need to check if an array contains duplicated values.
Lets say I have the following array of arrays:
array = [
{ id: 123, name: 'Emily', address: 'UK' },
{ id: 123, name: 'Ross', address: 'USA' },
{ id: 157, name: 'Joey', address: 'Italy' },
];
As you can see, I have 2 arrays, aving the same ID id=123, and I need to detect these 2 rows, so we can clean the data we have.
P.S. I am only interested in checking duplication on IDs and Names at the same time.
I did the following logic, but it doesn't make sense, as it returns rows more than the existed ones:
ngOnInit() {
this.array.forEach((row) => {
this.array.find(element => {
if (element['id'] === row['id']) {
console.log(row)
}
})
})
}
The output is like:
123 Emily
123 Emily
123 Ross
123 Ross
157 Joey
My needed out output is as the following:
123 Emily
123 Ross
Here is a stackblitz.
You can use filter and map like this to get a distinct-like behaviour:
function distinct(myArray, prop) {
return myArray.filter((obj, pos, arr) => {
return arr.map(mapObj => mapObj[prop]).indexOf(obj[prop]) === pos;
});
}

Why can’t I access object properties in a for-in loop over an array of objects? [duplicate]

This question already has answers here:
What is the difference between ( for... in ) and ( for... of ) statements?
(18 answers)
Closed 4 years ago.
I am currently utilizing the SheetSU API to read data from a Google Sheets file.
var members = Sheetsu.read("URL", {}, successFunc);
For now, the successFunc is a function that takes the input data to console.log() to me.
The data imports like this: (14){{..},{..},{..}....} and each object within this dictionary looks something like this:
0: {Name: "First Last", ID: "12536723", Status: "Member"}
1: {Name: "First Last", ID: "12371238", Status: "Member"}
...
14: {Name: "First Last", ID: "12341228", Status: "Member"}
I need to pull out the values of Name and ID, however:
for (var x in data) {
console.log(x)
}
= 0, 1, 2, ..., 14 (as ints not objects)
for (var x in data) {
console.log(x.Name)
}
= undefined, undefined, ..., undefined
x is not a value It is a index like 0,1 ...14, you can use of for that or use data[x] inside in.
var data = [{
Name: "First Last",
ID: "12536723",
Status: "Member"
}, {
Name: "First Last",
ID: "12371238",
Status: "Member"
},
{
Name: "First Last",
ID: "12341228",
Status: "Member"
}
]
for (var x in data) {
console.log(data[x].Name); //x is not a value It is a index like 0,1 ...14
}
for (var x of data) {
console.log(x.Name); // use of which prints x as object not index
}

Categories