node.js find attributes with same value in multiple JSON files - javascript

Hey I would like to ask if it is possible to find lets say in 10 JSON files which are looking like this:
1.json:
{
"name": "Miami",
"attributes": [
{
"att_type": "People",
"value": "1000"
},
{
"att_type": "Cars",
"value": 300
}
]
}
2.json:
{
"name": "New York",
"attributes": [
{
"att_type": "People",
"value": "5000"
},
{
"att_type": "Cars",
"value": 900
}
]
}
And so on... just different attribute values.
Lets say I want to find only towns with People > 2500 and I'm not even sure if it is possible or do I need to upload the json files to some database perhaps?
Thank you.

const data = [{
"name": "Miami",
"attributes": [{
"att_type": "People",
"value": "1000"
},
{
"att_type": "Cars",
"value": 300
}
]
},
{
"name": "New York",
"attributes": [{
"att_type": "People",
"value": "5000"
},
{
"att_type": "Cars",
"value": 900
}
]
}
]
const result = data
// find cities with attribute `People` with value greater than 2500
.filter(d => +d.attributes.find(attr => attr.att_type === 'People').value > 2500)
// get name of the city
.map(d => d.name)
console.log(result)

Related

Filter and get data from JSON

How to get the data value from the list, the size of the array, the main thing is not through the index, because the order of the arrays can change and I can get specific data from the code === "size". Unfortunately, the structure cannot be changed. It came to mind only through the filter, by index, but it is impossible
The result should be 100 150
https://jsoneditoronline.org/#left=cloud.b10638604c214b189f87747414e06035
[
[
"color",
{
"name": "Цвет",
"code": "color",
"list": [
{
"value": "Зеленый"
},
{
"value": "Красный"
}
]
}
],
[
"size",
{
"name": "Размер",
"code": "size",
"list": [
{
"value": "100"
},
{
"value": "150"
}
]
}
]
]
This data structure is terrible, but a quick fix could be something like this
const data = [
[
"color",
{
"name": "Цвет",
"code": "color",
"list": [
{
"value": "Зеленый"
},
{
"value": "Красный"
}
]
}
],
[
"size",
{
"name": "Размер",
"code": "size",
"list": [
{
"value": "100"
},
{
"value": "150"
}
]
}
]
]
const size = data.filter(element => element[1].code === 'size')[0][1].list.map(element => element.value)
console.log(size)

How can I .filter an object by elements within an array inside an object?

I've been playing around trying to learn in an API project using Postman and conducting tests using JavaScript. So far, I have succeeded with the help of reading on websites and watching YouTube videos. Of course, previous tests and playing around have been fairly easy but now I came to a stop. I really tried to figure this out for several weeks but I need further guidance, a push in the right direction or direct help.
What I'm trying to do is to filter out some of the response to only view objects that contain specific data.
To do that, I'm using a filter where I want all products containing a specific value inside an array "product_option_values".
My first approach was to see if I could sort products having any values from the first array, and it worked. It filters just fine.
var filterSmall = jsonData.products.filter(fs => fs.associations.product_option_values);
My next approach was to get to my goal of filtering out products according to specific values inside this array. I tried many simple .(dot) combinations and pointing to [index] to access it without any luck. (I must add that I know how to access this from a specific product, but that way doesn't work when filtering).
I've also tried other approaches such as:
var filterSmall = jsonData.products.filter(fs => fs.associations["product_option_values", 0, "name"] === "S");
and other similar combinations.
This is a very shortened sample of the structure of "products" which in its full form consists of 20 products and far more values inside of it:
{
"products": [
{
"id": 16,
"manufacturer_name": "Graphic Corner",
"quantity": "0",
"price": "12.900000",
"indexed": "1",
"name": "Mountain fox notebook",
"associations": {
"categories": [
{
"id": "2"
},
{
"id": "6"
}
],
"product_option_values": [
{
"id": "22"
},
{
"id": "23"
}
]
}
},
{
"id": 17,
"manufacturer_name": "Graphic Corner",
"quantity": "0",
"price": "12.900000",
"indexed": "1",
"name": "Brown bear notebook",
"associations": {
"categories": [
{
"id": "2"
},
{
"id": "6"
}
],
"product_option_values": [
{
"id": "23"
},
{
"id": "24"
}
]
}
}
]
}
and here is a small and expanded sample from product_option_values:
{
"product_option_values": [
{
"id": 1,
"id_attribute_group": "1",
"color": "",
"position": "0",
"name": "S"
},
{
"id": 2,
"id_attribute_group": "1",
"color": "",
"position": "1",
"name": "M"
},
{
"id": 3,
"id_attribute_group": "1",
"color": "",
"position": "2",
"name": "L"
}
]
}
How do I proceed? Did I do anything correct or even close to it?
Perhaps I've been staring at this for too long.
Thanks in advance.
If you want to compare nested attributes you have to transform the objects (e.g. by using a map operation), so that the relevant attributes are easily accessible for a comparison. If you want to filter by product_option_value id, you could do something like this:
const jsonData = {
"products": [
{
"id": 16,
"manufacturer_name": "Graphic Corner",
"quantity": "0",
"price": "12.900000",
"indexed": "1",
"name": "Mountain fox notebook",
"associations": {
"categories": [
{
"id": "2"
},
{
"id": "6"
}
],
"product_option_values": [
{
"id": "22"
},
{
"id": "23"
}
]
}
},
{
"id": 17,
"manufacturer_name": "Graphic Corner",
"quantity": "0",
"price": "12.900000",
"indexed": "1",
"name": "Brown bear notebook",
"associations": {
"categories": [
{
"id": "2"
},
{
"id": "6"
}
],
"product_option_values": [
{
"id": "23"
},
{
"id": "24"
}
]
}
}
]
};
const sample = {
"product_option_values": [
{
"id": 22,
"id_attribute_group": "1",
"color": "",
"position": "0",
"name": "S"
},
{
"id": 2,
"id_attribute_group": "1",
"color": "",
"position": "1",
"name": "M"
},
{
"id": 3,
"id_attribute_group": "1",
"color": "",
"position": "2",
"name": "L"
}
]
};
const ids = sample.product_option_values.map((el) => String(el.id));
console.log(ids);
const filtered = jsonData.products.filter((fs) => fs.associations.product_option_values.map((e) => e.id).some((f) => ids.includes(f)));
console.log(filtered);

Javascript get data from within brackets

I understand that I can extract for example the currency ("EUR") with order.unitPricePaid.currency, but how do I extract for example the tax number ("GB08713331")?
Below is the data I have:
{
"unitPricePaid": {
"currency": "EUR",
"value": "59.00"
},
"formSubmission": [
{
"label": "User",
"value": "Creatively"
}, {
"label": "Tax number",
"value": "GB08713331"
}
]
}
order.formSubmission[1].value
What this does is it looks at the "formSubmission" as an array. So, you can access each element how it is. Since "label: Tax Number" and "value: GB08713331" are in the second element of the array, you use "formSubmission[1]"
You can get the tax number using Array.prototype.find on the formSubmission property.
const data = {
"unitPricePaid": {
"currency": "EUR",
"value": "59.00"
},
"formSubmission": [{
"label": "User",
"value": "Creatively"
}, {
"label": "Tax number",
"value": "GB08713331"
}]
};
const taxNumber = data.formSubmission
.find(field => field.label === 'Tax number').value;
console.log(`Tax number = "${taxNumber}"`);
Assuming this object:
var order = { "unitPricePaid": { "currency": "EUR", "value": "59.00" }, "formSubmission": [{ "label": "User", "value": "Creatively" }, { "label": "Tax number", "value": "GB08713331" }] }
the path to that value would be order.formSubmission[1].value. The [1] means we are accessing the element at index 1 of the array (index 0 would be the first element).

Push keys and values dynamically in Typescript

I want to push into array of objects keys and values dynamically. I thought that was pretty simple, something like:
list.forEach(element => {
element["carAttributes"].map((o) =>
{
this.car.push(
{
o.Name: o.Value
}
);
})
});
but it's not working because { o: any; } is not assignable to type CarInterface where CarInterface is:
export interface CarInterface {
"name": string;
}
o returns me something like
{"Name": "name", "Value": "Mercedes"}
what I have to do is take the value of the key Name and the value of the key Value and put everything in one object like:
{"name": "Mercedes"}
I have many values so i have to push everything in this.car list. Is that possible?
EDIT:
It could be something like that
{
"Cars": [{
"carAttributes": [{
"Name": "name",
"Value": "Mercedes"
}, {
"Name": "color",
"Value": "grey"
}, {
"Name": "model",
"Value": "A220"
}],
"available": true,
},{
"carAttributes": [{
"Name": "name",
"Value": "Mercedes"
}, {
"Name": "color",
"Value": "red"
}, {
"Name": "model",
"Value": "B250E"
}],
"available": false,
}]
}
I need to create an array of objects for each car that has this kind of structure
[
{"name": "Mercedes", "color": "grey", "model": "A220"},
{"name": "Mercedes", "color": "red", "model": "B250E"},
]
That's it.

Objects keys value to be updated with arrays value

Below is my Attempt, I have an object which has array of objects within it, it has a field: 'positionTitle'.
I also have an array of objects which also has a 'positionTitle'
They both have similar data I want all of the values for the positionTitles in my 'individualsData' to go into 'graphData' and be able to now use this new graphData!
I think my attempt is wrong its treating them both as arrays?
Thanks, Dale
graphData = {
"engagementAreas": [{
"id": "1",
"engagementTypes": [{
"name": "forestry",
"engagements": []
},
{
"name": "houses",
"engagements": [{
"name": "engagement1",
"members": [{
"position": {
"id": "3434",
"positionTitle": "Manager"
}
}]
}]
}
]
}]
}, {
"name": "landscaping",
"engagements": [{
"name": "engagement1343",
"members": [{
"position": {
"id": "4545",
"positionTitle": "Senior Manager"
}
}]
}]
}
IndividualData = [{
"account": {
"id": "001b000003WnPy1AAF",
"fullName": "Adnan A. Khan"
},
"positions": [{
"id": "a16b0000004AxeBAAS",
"organizationId": "001b0000005gxmlAAA",
"organizationName": "a",
"positionTitle": "Senior Manager, Energy",
"positionLevel": "5-Middle Management & Advisers",
"isPrimary": true,
"startDate": "2016-10-07",
"endDate": null
}]
}, {
"account": {
"id": "0010X000048DDMsQAO",
"fullName": "Christine Leong"
},
"positions": [{
"id": "a160X000004nKfhQAE",
"organizationId": "001b0000005gxmlAAA",
"organizationName": "a",
"positionTitle": "Managing Director",
"positionLevel": "4-Head of Business Unit/Head of Region",
"isPrimary": true,
"startDate": "2018-03-05",
"endDate": null
}]
}
What I expect to see:
NEWgraphData = {
"engagementAreas": [{
"id": "1",
"engagementTypes": [{
"name": "forestry",
"engagements": []
},
{
"name": "houses",
"engagements": [{
"name": "engagement1",
"members": [{
"position": {
"id": "3434",
"positionTitle": "Senior Manager, Energy" <== from individualsdata
}
}]
}]
}
]
}]
}, {
"name": "landscaping",
"engagements": [{
"name": "engagement1343",
"members": [{
"position": {
"id": "4545",
"positionTitle": "Managing Director" <== also from individuals data
}
}]
}]
}
graphData.engagementAreas.map((el, i) => {
el.engagementTypes.engagements.members.position.positionTitle = individualsData.positions.positionTitle;
return el;
})
As engagementTypes, engagements and members properties are also array of objects, you have to loop them as well as below.
graphData.engagementAreas.map((el, i) => {
el.engagementTypes.forEach((et) => {
et.engagements.forEach((eg) => {
eg.members.forEach((mem) => {
mem.position.positionTitle = individualsData.positions.positionTitle; // make sure this is correct
});
});
});
return el;
})
Here is the solution. but you have to choose what individual data item will be the pick for the positionTitle
graphData.engagementAreas.map((el, i) => {
return el.engagementTypes.map((el2,i2) => {
return el2.engagements.map( (el3,i3) => {
return el3.members.map((el4,i4) => {
return el4.position.positionTitle =individualsDt[0].positions.[0].positionTitle;// take a look here, i just pick positionTitle staticly
})
})
})
});
see implementation in console here enter link description here

Categories

Resources