Finding values from a nested object - arrays

I have an array like so:
[
{
lookups: [],
rows: [{data: {a: 1, b: 2}}, {data: {a: 3, b: 4}}],
title: "Table 1",
columns: [{name: "a"}, {name: "b"}]
},
{
lookups: [],
rows: [{data: {c: 5, d: 6}}, {data: {c: 7, d: 8}}],
title: "Table 2",
columns: [{name: "c"}, {name: "d"}]
}
]
After I select the correct object (which I am using Array.find() using the title to do). I need to be able to go through the rows array and try to get each value for a particular string that I have (e.g. If I have a string value of "a" then I would like to get the values 1 and 3 back).
Help would be very much appreciated.
Thanks for your time.

Use simple mapping. Suppose you have property name stored in variable named prop
data = obj.rows.map(({data}) => data[prop])

Related

How sort when have array with name and score mongodb

I have a document with users and each user haves "categories" and "tags", in both as at next:
[
{user_id: 1, categories:[
{category: uuidv4x1, score:75},
{category: uuidv4x2, score:67}
],
tags: [
{tag: "1ne", score:15},
{tag: "subjetive", score:89}
]}
How can order by category, tag but too by score, if i have:
[
{id: 1, category: uuidv4x6, tags:["1ne", "optimized"]},
{id: 2, category: uuidv4x2, tags:["1ne", "subjetive"]},
{id: 3, category: uuidv4x1, tags:["home", "car"]}
]
Before to all, thanks so much.
I expect sort my find for better matches, customize articles.

Using Map Object to Render React Components in Order

I receive an array of product objects like such from my backend.
products = [
{id: 1, price: 10, category: "food", name: "apples"},
{id: 2, price: 5, category: "supplies", name: "spoons"},
{id: 3, price: 15, category: "pets", name: "cat treats"},
{id: 4, price: 9, category: "food", name: "oranges"},
// ...
]
I am looking to render all my products as strips based on their category in a specific order of priority (similar to how Netflix aligns their shows). I don't want them displayed in random order, such as the category strip of food being at the top, the next category strip being pets, etc.
I currently decided to create a map object since it keeps order as followed:
let filteredProducts = new Map([
["food", []],
["pets", []],
["supplies", []],
// ...
])
for (let product of products) {
let category = product["category"]
filteredProducts.get(category).push(product)
}
The goal is to then iterate through the map object and return multiple React Component of category strips from top to bottom.
My question is, I can't help but feel that there are more efficient ways to render this as I don't see map objects being utilized commonly. Are there more efficient and frequently used ways to filter and render by priority or is this in the right direction?
I do believe there is more easier way to do this using Array sort function.
Please consider the snippet below.
const products = [
{id: 1, price: 10, category: "food", name: "apples"},
{id: 2, price: 5, category: "supplies", name: "spoons"},
{id: 3, price: 15, category: "pets", name: "cat treats"},
{id: 4, price: 9, category: "food", name: "oranges"},
// ...
]
const priority = [
'food',
'supplies',
'pets'
]
const orderedProducts = products.sort(
(p1, p2) => priority.indexOf(p1.category) - priority.indexOf(p2.category)
);
Let me know if this looks much cleaner & easier approach.

merge and display array of object data in table using react js

I need to merge array1 and array2 , display in a table
array1= [ {id: 1,event:party, oldcity: Singapore}
{id: 2, event:fest,oldcity:Paris }]
array2= [ {id: 1,event:party, newcity: bombay}
{id: 2, event:fest,newcity:madras }]
output of merge array
mergearray= [ {id: 1,event:party,oldcity: Singapore,newcity: bombay}
{id: 2, event:fest,oldcity:Paris,newcity:madras }]
display mergearray in table
id event oldcity newcity
1 party singapore bombay
2 fest paris madras
if i merge the array
mergearray[{id: 1,event:party, oldcity: Singapore}
{id: 2, event:fest,oldcity:Paris }
{id: 1,event:party, newcity: bombay }
{id: 2, event:fest,newcity:madras }]
but am expecting to be like this
mergearray= [ {id: 1,event:party,oldcity: Singapore,newcity: bombay}
{id: 2, event:fest,oldcity:Paris,newcity:madras }]
How do you merge the arrays ?
const array1 = [{
id: 1,
event: 'party',
oldcity: 'Singapore'
}, {
id: 2,
event: 'fest',
oldcity: 'Paris'
}];
const array2 = [{
id: 1,
event: 'party',
newcity: 'bombay'
}, {
id: 2,
event: 'fest',
newcity: 'madras'
}];
// I would merge arrays this way
const mergedArray = [...array1, ...array2];
console.log('mergedArray: ', mergedArray);

Azure Cosmos DocumentDB - return array from 1 property in sub array

I've searched around to see if anybody needed this before but couldn't find much.
I have the following json objects in Azure DocumentDB collection:
{
personID: 1,
name: "Bruce",
surname: "Dickinson",
items: [
{
itemID: 1,
itemType: "A",
name: 'Item 1'
},
{
itemID: 2,
itemType: "A",
name: 'Item 2'
},
{
itemID: 3,
itemType: "B",
name: 'Item 3'
}
]
}
The collection has many Persons in it. personID 2, 3, 4 ......100....1000 and so on.
I would like to write a SQL query to extract an array of itemIDs for personID = 1 and items with itemType = 'A'. I want the result to look like this:
itemIDs: [
1,
2
]
or simply:
[
1,
2
]
Has anybody ever tried doing this before? Is it even possible?
Someone with more SQL foo than me might know a way to do it with just SQL, but I would use a user-defined function (UDF) to do it.
Here is the SQL:
SELECT VALUE udf.getItemIDArray(c) FROM collection c WHERE <your clause>
Here's what the UDF might look like:
function getItemIDArray (o) {
output = [];
for (item in o.items) {
output.push(item.itemID)
}
return output
}

How to group array of hashes by one key and merge inner keys and arrays

How can I transform an array of hashes:
[{
id: 115,
ref_id: 440000000000337,
properties: [{name: "test"}],
type: "content"
},{
id: 116,
ref_id: 440000000000337,
properties: [{name: "asdf"}],
type: "content"
}]
to get the desired result:
{
id: 440000000000337
type: "content"
properties: [{name: "test"}, {name: "asdf"}]
}
in one block smarter then [sic] in the example? Would it be best to obtain this [sic] results with ruby functions?
in = _
out = {properties: []}
in.map {|i| out[:id] = i[:ref_id]; out[:properties] << i[:properties]; out[:type] = i[:type]}
out[:properties].flatten!
You cannot use the variable name in since it is a keyword. I will use iin instead.
out = {
id: iin.last[:ref_id],
type: iin.last[:type],
properties: iin.flat_map{|e| e[:properties]}
}

Resources