How to find the lowest number an object - javascript

I have an object like this:
"data": [
{
"type": "products",
"id": "2021-01-04.1.1",
"attributes": {
"name": "product 1",
"descriptions": "some description",
"image": null,
"date": "2021-01-04",
"valid_from": null,
"valig_untill": null,
"xparc_paid_product_id": 1,
"xparc_unpaid_product_id": 1,
"xparc_access_id": 1,
"stock": null,
"variations": [
{
"id": 1,
"product_template_id": "1",
"name": "child ticket",
"description": "Only for children!",
"price_excl_vat": 1,
"created_at": "2021-09-15T13:16:00.000000Z",
"updated_at": "2021-09-15T13:16:00.000000Z"
},
{
"id": 2,
"product_template_id": "1",
"name": "Adults",
"description": "Not for children!",
"price_excl_vat": 2,
"created_at": "2021-09-15T13:16:10.000000Z",
"updated_at": "2021-09-15T13:16:10.000000Z"
}
]
},
"links": {
"self": "..."
}
}, ...
]
So as you see data has object inside. What I am trying to do is I want to find the cheapest price (price_excl_vat) inside variations. But I don't know how I should return. I have started to create this function:
priceCalculationCheapest(obj){
let arr = Object.values(obj);
let min = Math.min(...arr);
return min;
},
But I cannot go further. Could you please help me about this?
Thanks

You mean this
const obj = {
data: {
"variations": [{
"id": 1,
"product_template_id": "1",
"name": "child ticket",
"description": "Only for children!",
"price_excl_vat": 1,
"created_at": "2021-09-15T13:16:00.000000Z",
"updated_at": "2021-09-15T13:16:00.000000Z"
},
{
"id": 2,
"product_template_id": "1",
"name": "Adults",
"description": "Not for children!",
"price_excl_vat": 2,
"created_at": "2021-09-15T13:16:10.000000Z",
"updated_at": "2021-09-15T13:16:10.000000Z"
}
]
}
}
const price = Math.min(...obj.data.variations
.map(({price_excl_vat}) => price_excl_vat ))
console.log(price)

try below code.
var obj = {
"data": [{
"type": "products",
"id": "2021-01-04.1.1",
"attributes": {
"name": "product 1",
"descriptions": "some description",
"image": null,
"date": "2021-01-04",
"valid_from": null,
"valig_untill": null,
"xparc_paid_product_id": 1,
"xparc_unpaid_product_id": 1,
"xparc_access_id": 1,
"stock": null,
"variations": [{
"id": 1,
"product_template_id": "1",
"name": "child ticket",
"description": "Only for children!",
"price_excl_vat": 1,
"created_at": "2021-09-15T13:16:00.000000Z",
"updated_at": "2021-09-15T13:16:00.000000Z"
},
{
"id": 2,
"product_template_id": "1",
"name": "Adults",
"description": "Not for children!",
"price_excl_vat": 2,
"created_at": "2021-09-15T13:16:10.000000Z",
"updated_at": "2021-09-15T13:16:10.000000Z"
}
]
},
"links": {
"self": "..."
}
}]
}
function priceCalculationCheapest(arr) {
let small = arr.reduce((first, second) => first.price_excl_vat < second.price_excl_vat ? first : second);
return small;
};
console.log(priceCalculationCheapest(obj.data[0].attributes.variations));

Best and easy way to sort array. If you have fewer records. which will be O(nlogn). Else you can use linear for loop to find min or max records.
Sample:
const obj = {
data: {
variations: [
{
id: 1,
product_template_id: "1",
name: "child ticket",
description: "Only for children!",
price_excl_vat: 1,
created_at: "2021-09-15T13:16:00.000000Z",
updated_at: "2021-09-15T13:16:00.000000Z",
},
{
id: 2,
product_template_id: "1",
name: "Adults",
description: "Not for children!",
price_excl_vat: 2,
created_at: "2021-09-15T13:16:10.000000Z",
updated_at: "2021-09-15T13:16:10.000000Z",
},
],
},
};
const sortBy = (key, data = []) => {
return data.sort((v1, v2) => {
return v1[key] - v2[key]; // for reverse, max to min v2[key]-v1[key]
});
};
const priceSorted = sortBy("product_template_id", obj.data.variations);
console.log(priceSorted[0]); // min
console.log(priceSorted[obj.data.variations.length - 1]); // max

Iterate through each object in array and compare the price to find minimum.
const product = {
data: {
"variations": [{
"id": 1,
"product_template_id": "1",
"name": "child ticket",
"description": "Only for children!",
"price_excl_vat": 1,
"created_at": "2021-09-15T13:16:00.000000Z",
"updated_at": "2021-09-15T13:16:00.000000Z"
},
{
"id": 2,
"product_template_id": "1",
"name": "Adults",
"description": "Not for children!",
"price_excl_vat": 2,
"created_at": "2021-09-15T13:16:10.000000Z",
"updated_at": "2021-09-15T13:16:10.000000Z"
}
]
}
};
const getMinPrice = (list) => {
let minPrice = list[0].price_excl_vat;
list.forEach(obj => {
if (obj.price_excl_vat < minPrice) {
minPrice = obj.price_excl_vat;
}
});
return minPrice;
}
console.log(getMinPrice(product.data.variations));

Related

JS Merging object properties from an array of objects by matching ids

I have two arrays of objects. I want to look at the first one, find typeId then look in the second array for the match (states.typeId == stateTypes.id) then merge those properties into the first array's found match object. If the properties have same key append "stateType" to the property name if not just bring it over. I think an example would best explain it.
Array of objects
"states": [
{
"id": 1,
"typeId": 1,
"name": "CREATED",
"description": "Created",
"label": "Created",
"perviousNotMatchKey": "Text"
},
{
"id": 2,
"typeId": 3,
"name": "ASSIGNED",
"description": "Assigned",
"label": "Assigned",
"perviousNotMatchKey": "Text"
},
{
"id": 3,
"typeId": 3,
"name": "COMPLETED",
"description": "Completed",
"label": "Completed",
"perviousNotMatchKey": "Text"
}
],
"stateTypes": [
{
"id": 1,
"name": "PENDING",
"description": "Pending",
"label": "Pending",
"newIncomingKey": "Text"
},
{
"id": 2,
"name": "IN_PROGRESS",
"description": "In Progress",
"label": "In Progress",
"newIncomingKey": "Text"
},
{
"id": 3,
"name": "COMPLETED",
"description": "Completed",
"label": "Completed",
"newIncomingKey": "Text"
}
],
Wanted array
"newArray": [
{
"id": 1,
"typeId": 1,
"name": "CREATED",
"description": "Created",
"label": "Created",
"perviousNotMatchKey": "Text",
"newIncomingKey": "Text",
"stageType-id": 1,
"stageType-name": "PENDING",
"stageType-description": "Pending",
"stageType-label": "Pending"
},
{
"id": 2,
"typeId": 3,
"name": "ASSIGNED",
"description": "Assigned",
"label": "Assigned",
"perviousNotMatchKey": "Text",
"newIncomingKey": "Text",
"stageType-id": 3,
"stageType-name": "COMPLETED",
"stageType-description": "Completed",
"stageType-label": "Completed"
},
{
"id": 3,
"typeId": 2,
"name": "COMPLETED",
"description": "Completed",
"label": "Completed",
"perviousNotMatchKey": "Text",
"newIncomingKey": "Text",
"stageType-id": 2,
"stageType-name": "IN_PROGRESS",
"stageType-description": "In Progress",
"stageType-label": "In Progress"
}
],
Something like this:
const state = [
{
"id": 1,
"typeId": 1,
"name": "CREATED",
"description": "Created",
"label": "Created",
"perviousNotMatchKey": "Text"
},
{
"id": 2,
"typeId": 3,
"name": "ASSIGNED",
"description": "Assigned",
"label": "Assigned",
"perviousNotMatchKey": "Text"
},
{
"id": 3,
"typeId": 3,
"name": "COMPLETED",
"description": "Completed",
"label": "Completed",
"perviousNotMatchKey": "Text"
}
];
const stateTypes = [
{
"id": 1,
"name": "PENDING",
"description": "Pending",
"label": "Pending",
"newIncomingKey": "Text"
},
{
"id": 2,
"name": "IN_PROGRESS",
"description": "In Progress",
"label": "In Progress",
"newIncomingKey": "Text"
},
{
"id": 3,
"name": "COMPLETED",
"description": "Completed",
"label": "Completed",
"newIncomingKey": "Text"
}
];
const temp = {};
for (const obj1 of state) {
temp[obj1.id] = { ...obj1 };
}
for (const obj2 of stateTypes) {
const destination = temp[obj2.id];
for (const [key, value] of Object.entries(obj2)) {
if (destination[key]) destination[`stateTypes-${key}`] = value;
else destination[key] = value;
}
}
const newArray = Object.values(temp);
console.log(newArray);

How to make advanced filter with jQuery

The Question;
I want to filter by properties.
I have json data coming from the api and I want to filter that data.(If there is a way to do with laravel I would like to try that too)
Json Data;
[
{
"id": 1,
"title": "Deneme Batarya",
"description": "<p>Deneme Batarya</p>",
"slug": "deneme-batarya",
"brand_id": 4,
"deleted_at": null,
"created_at": "2021-02-22 19:43:02",
"updated_at": "2021-02-22 19:43:02",
"categories": [
{
"id": 3,
"parent_id": null,
"title": "Giyilebilir Teknoloji",
"description": null,
"slug": "giyilebilir-teknoloji",
"media": "public/media/slider/ms-10.jpg",
"level": 0,
"deleted_at": null,
"created_at": null,
"updated_at": null,
"pivot": {
"product_id": 1,
"category_id": 3
}
},
{
"id": 26,
"parent_id": 3,
"title": "Akıllı Bileklik",
"description": null,
"slug": "akilli-bileklik",
"media": "public/media/slider/ms-10.jpg",
"level": 2,
"deleted_at": null,
"created_at": null,
"updated_at": null,
"pivot": {
"product_id": 1,
"category_id": 26
}
}
],
"images": [
{
"id": 3,
"product_id": 1,
"product_detail_id": 1,
"sku": "model-45b",
"image": "public/media/product/1-model-45b864.png",
"level": 1,
"deleted_at": null,
"created_at": "2021-02-24 19:49:16",
"updated_at": "2021-02-24 19:49:16"
},
{
"id": 4,
"product_id": 1,
"product_detail_id": null,
"sku": null,
"image": "public/media/product/1--501.jpeg",
"level": 1,
"deleted_at": null,
"created_at": "2021-02-24 19:53:27",
"updated_at": "2021-02-24 19:53:27"
}
],
"details": [
{
"id": 1,
"product_id": 1,
"qty": 5,
"status": "Available",
"price": "56.00",
"discount": 5,
"sku": "model-45b",
"start": "2021-02-22 00:00:00",
"end": "2021-02-23 00:00:00",
"deleted_at": null,
"created_at": "2021-02-22 19:43:27",
"updated_at": "2021-02-22 19:43:27"
},
{
"id": 2,
"product_id": 1,
"qty": 6,
"status": "Available",
"price": "90.00",
"discount": 4,
"sku": "model-45a",
"start": "2021-02-25 00:00:00",
"end": "2021-02-26 00:00:00",
"deleted_at": null,
"created_at": "2021-02-24 19:26:48",
"updated_at": "2021-02-24 19:26:48"
}
],
"properties": [
{
"id": 1,
"product_id": 1,
"product_detail_id": 1,
"property_id": 7,
"parent_id": 1,
"created_at": "2021-02-22 19:43:27",
"updated_at": "2021-02-22 19:43:27",
"property": {
"id": 7,
"parent_id": 1,
"title": "Green",
"slug": "green",
"value": "#008000",
"created_at": null,
"updated_at": null
},
"parent": {
"id": 1,
"parent_id": null,
"title": "Color",
"slug": "color",
"value": null,
"created_at": null,
"updated_at": null
}
},
{
"id": 2,
"product_id": 1,
"product_detail_id": 1,
"property_id": 20,
"parent_id": 3,
"created_at": "2021-02-22 19:43:27",
"updated_at": "2021-02-22 19:43:27",
"property": {
"id": 20,
"parent_id": 3,
"title": "1 TB",
"slug": "1-tb",
"value": "1 TB",
"created_at": null,
"updated_at": null
},
"parent": {
"id": 3,
"parent_id": null,
"title": "Store Capacity",
"slug": "store-capacity",
"value": null,
"created_at": null,
"updated_at": null
}
},
{
"id": 3,
"product_id": 1,
"product_detail_id": 2,
"property_id": 7,
"parent_id": 1,
"created_at": "2021-02-24 19:26:48",
"updated_at": "2021-02-24 19:26:48",
"property": {
"id": 7,
"parent_id": 1,
"title": "Green",
"slug": "green",
"value": "#008000",
"created_at": null,
"updated_at": null
},
"parent": {
"id": 1,
"parent_id": null,
"title": "Color",
"slug": "color",
"value": null,
"created_at": null,
"updated_at": null
}
},
{
"id": 4,
"product_id": 1,
"product_detail_id": 2,
"property_id": 12,
"parent_id": 2,
"created_at": "2021-02-24 19:26:48",
"updated_at": "2021-02-24 19:26:48",
"property": {
"id": 12,
"parent_id": 2,
"title": "L",
"slug": "l",
"value": "Large",
"created_at": null,
"updated_at": null
},
"parent": {
"id": 2,
"parent_id": null,
"title": "Size",
"slug": "size",
"value": null,
"created_at": null,
"updated_at": null
}
},
{
"id": 5,
"product_id": 1,
"product_detail_id": 2,
"property_id": 17,
"parent_id": 3,
"created_at": "2021-02-24 19:26:48",
"updated_at": "2021-02-24 19:26:48",
"property": {
"id": 17,
"parent_id": 3,
"title": "64 GB",
"slug": "64-gb",
"value": "64 GB",
"created_at": null,
"updated_at": null
},
"parent": {
"id": 3,
"parent_id": null,
"title": "Store Capacity",
"slug": "store-capacity",
"value": null,
"created_at": null,
"updated_at": null
}
}
]
}
]
I don't know the properties that user added because it is dynamic. So I'm reaching the properties like this.
function makeQueryString() {
function keyValues(idPref) {
var key = idPref,
values = $('#' + idPref + ' input[id^="' + idPref + '"]').map(function() {
if (this.value !== this.defaultValue || this.checked !== this.defaultChecked) {
return this.value;
}
}).get().join(',');
return key && values.length ? key + '=' + values : '';
}
return $('form#property-filter span[id]').map(function(){
let keyValue = keyValues(this.id);
if (keyValue != ''){
return keyValue;
}
}).get().join('&');
}
Properties Result;
id of properties with name
color=6,7&store-capacity=15,16,17&ram=21,22,23
EDIT
The main problem is make a filter using property result.
For example when user select Red And Blue color and click filter button it will return color=6,7 and I should filter my json data with this and bring only if include color 6 or 7. At the same time if user select red, 64GB, 128GB then it should return only if red color with 64 or 128.
I tried so far;
function filterProducts(prop) {
// prop = color=6,7&store-capacity=15,16,17&ram=21,22
prop = prop.split('&');
let x = [];
for (i=0; i<prop.length; i++){
x.push(prop[i].split('='));
}
let newProductsArr = [];
for (p=0; p<products.length; p++) {
for (k=0; k< products[p].properties.length; k++){
for (i=0; i<x.length; i++){
if(x[i][0] === products[p].properties[k].parent.slug){
let ids = x[i][1].split(',');
for (g=0; g<ids.length; i++){
if (products[p].properties[k].property_id === ids[g]){
newProductsArr.push(products[p])
}
}
}
}
}
}
}

Filter Array of user objects by array of roles in Javascript

I have an array of user objects.
I want to filter them based on the array of user roles.
filter = ['ROLE_SELLER', 'ROLE_BANK', 'ROLE_CPF', 'ROLE_SLA', 'ROLE_LDAU']
const users = [{
"id": 1,
"email": "user1#test.com",
"name": "User1",
"roles": [{
"id": 1,
"code": "ROLE_ADMINISTRATOR",
"name": "Administrator"
},
{
"id": 2,
"code": "ROLE_SELLER",
"name": "Seller"
}
]
}, {
"id": 2,
"email": "user2#test.com",
"name": "User2",
"roles": [{
"id": 1,
"code": "ROLE_ADMINISTRATOR",
"name": "Administrator"
}]
}, {
"id": 3,
"email": "user3#test.com",
"name": "User3",
"roles": [{
"id": 1,
"code": "ROLE_ADMINISTRATOR",
"name": "Administrator"
}]
}, {
"id": 4,
"email": "user4#test.com",
"name": "User4",
"roles": [{
"id": 1,
"code": "ROLE_ADMINISTRATOR",
"name": "Administrator"
},
{
"id": 2,
"code": "ROLE_SELLER",
"name": "Seller"
}
]
}, {
"id": 5,
"email": "user5#test.com",
"name": "User5",
"roles": [{
"id": 5,
"code": "ROLE_LAWYER",
"name": "Lawyer"
}]
}, {
"id": 6,
"email": "user6#test.com",
"name": "User6",
"roles": [{
"id": 2,
"code": "ROLE_SELLER",
"name": "Seller"
}]
},
{
"id": 7,
"email": "user7#test.com",
"name": "User7",
"roles": [{
"id": 9,
"code": "ROLE_SLA",
"name": "sla"
}]
},
{
"id": 8,
"email": "user8#test.com",
"name": "User8",
"roles": [{
"id": 8,
"code": "ROLE_BANK",
"name": "Bank"
}]
},
{
"id": 9,
"email": "user9#test.com",
"name": "User9",
"roles": [{
"id": 7,
"code": "ROLE_CPF",
"name": "CPF"
}]
}
]
const filter = ['ROLE_SELLER', 'ROLE_BANK', 'ROLE_CPF', 'ROLE_SLA', 'ROLE_LDAU']
const filteredUsers = users.filter(user => !user.roles.find(role => filter.includes(role.id)))
console.log(filteredUsers)
Expected result
[{
"id": 1,
"email": "user1#test.com",
"name": "User1",
"roles": [{
"id": 1,
"code": "ROLE_ADMINISTRATOR",
"name": "Administrator"
},
{
"id": 2,
"code": "ROLE_SELLER",
"name": "Seller"
}
]
}, {
"id": 4,
"email": "user4#test.com",
"name": "User4",
"roles": [{
"id": 1,
"code": "ROLE_ADMINISTRATOR",
"name": "Administrator"
},
{
"id": 2,
"code": "ROLE_SELLER",
"name": "Seller"
}
]
}, {
"id": 6,
"email": "user6#test.com",
"name": "User6",
"roles": [{
"id": 2,
"code": "ROLE_SELLER",
"name": "Seller"
}]
},
{
"id": 7,
"email": "user7#test.com",
"name": "User7",
"roles": [{
"id": 9,
"code": "ROLE_SLA",
"name": "sla"
}]
},
{
"id": 8,
"email": "user8#test.com",
"name": "User8",
"roles": [{
"id": 8,
"code": "ROLE_BANK",
"name": "Bank"
}]
},
{
"id": 9,
"email": "user9#test.com",
"name": "User9",
"roles": [{
"id": 7,
"code": "ROLE_CPF",
"name": "CPF"
}]
}
]
I am trying to image what result you want to receive....
You want to filter of users by array with possible roles, another worlds if user has one of roles of filter array you want to pass his to 'filteredUsers' array?
filter.includes(role.id) - I guess it is wrong, may be you want to
filter by role.code?
Array.find() doesn't support Explorer
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
Array.includes() - doesn't support Explorer too.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
I think the reason why that was happening was that you are trying to match the role id with the 'filter' array which contains the role code. From what i observed from the expected answer, you are looking for user who has any one of their role fits the filter. So do the following will filter by role code and with the expected result
const filteredUsers = users.filter((user) => {
return user.roles.map(role=>filter.includes(role.code)).includes(true)
})
this line of code basically map the filter to every role object to every user, if their role code is included in the filter it will add a true to the array(return array of map()) and for the filter function if the map() return array contains true then true(so basically a || for the whole array)

Using angularjs forEach loops

I am getting this type of json in my $scope of angularjs:
$scope.someStuff = {
"id": 2,
"service": "bike",
"min": "22",
"per": "100",
"tax": "1",
"categoryservices": [
{
"id": 32,
"category": {
"id": 1,
"name": "software"
}
},
{
"id": 33,
"category": {
"id": 2,
"name": "hardware"
}
},
{
"id": 34,
"category": {
"id": 3,
"name": "waterwash"
}
}
]
}
I want to use angularjs forEach loop and i want to get only category name,
My expected output:
[{"name":"software"}, {"name":"hardware"}, {"name":"waterwash"}]
You can use Array.map()
The map() method creates a new array with the results of calling a provided function on every element in the calling array.
$scope.someStuff.categoryservices.map((x) => { return { name: x.category.name}})
var obj = {
"id": 2,
"service": "bike",
"min": "22",
"per": "100",
"tax": "1",
"categoryservices": [{
"id": 32,
"category": {
"id": 1,
"name": "software"
}
},
{
"id": 33,
"category": {
"id": 2,
"name": "hardware"
}
},
{
"id": 34,
"category": {
"id": 3,
"name": "waterwash"
}
}
]
};
console.log(obj.categoryservices.map((x) => {
return {
name: x.category.name
}
}))
You can use map method by passing a callback function as parameter.
const someStuff = { "id": 2, "service": "bike", "min": "22", "per": "100", "tax": "1", "categoryservices": [ { "id": 32, "category": { "id": 1, "name": "software" } }, { "id": 33, "category": { "id": 2, "name": "hardware" } }, { "id": 34, "category": { "id": 3, "name": "waterwash" } } ] }
let array = someStuff.categoryservices.map(function({category}){
return {'name' : category.name}
});
console.log(array);

Javascript sum of items in a complex object

I have some data that I'm counting and putting the totals into an array.
Here is the data and code:
var data = {
"cars": [
{
"id": "1",
"name": "name 1",
"thsub": [
{
"id": "11",
"name": "sub 1",
"stats": {
"items": 5,
},
"ions": null
},
{
"id": "22",
"name": "sub 2",
"stats": {
"items": 5,
},
"translations": null
}
],
"image": null
},
{
"id": "2",
"name": "name 2",
"thsub": [
{
"id": "33",
"name": "sub 43",
"stats": {
"items": 20,
},
"ions": null
},
{
"id": "44",
"name": "sub 76",
"stats": {
"items": 5,
},
"translations": null
}
],
"image": null
}
]
}
var thCount = [];
for(key in data.cars[0].thsub ){
if(data.cars[0].thsub[key].stats){
thCount.push(data.cars[0].thsub[key].stats.items);
}
}
console.log(thCount);
For some reason "thCount" is returning [5, 5] when the result should be: [10, 25]
where is the code going wrong?
The correct code for your problem is as pasted below:
**var count = [];
for(var i = 0; i < data.cars.length; i++){
countSum = 0;
for(key in data.cars[i].thsub){
countSum = countSum + data.cars[i].thsub[key].stats.items;
}
count.push(countSum);
}**
Try this code, will solve your problem.
You need another loop on cars.
var data = {
"cars": [{
"id": "1",
"name": "name 1",
"thsub": [{
"id": "11",
"name": "sub 1",
"stats": {
"items": 5,
},
"ions": null
}, {
"id": "22",
"name": "sub 2",
"stats": {
"items": 5,
},
"translations": null
}],
"image": null
},
{
"id": "2",
"name": "name 2",
"thsub": [{
"id": "33",
"name": "sub 43",
"stats": {
"items": 20,
},
"ions": null
}, {
"id": "44",
"name": "sub 76",
"stats": {
"items": 5,
},
"translations": null
}],
"image": null
}
]
}
var thCount = [];
for (var l = 0, m = data.cars.length; l < m; l++) {
thCount[l] = 0;
for (var i = 0, j = data.cars[l].thsub.length; i < j; i++) {
if (data.cars[l].thsub[i].stats) {
thCount[l]+=data.cars[l].thsub[i].stats.items;
}
}
}
console.log(thCount);
You should use reduce() and map() methods.Any of this using a callback function.
The reduce() method applies a function against an accumulator and each
value of the array (from left-to-right) to reduce it to a single
value.
The map() method creates a new array with the results of calling a
provided function on every element in this array.
var result=data.cars.map(function(item){
return item.thsub.reduce(function(a, b) { return a.stats.items + b.stats.items; });
});
var data = {
"cars": [
{
"id": "1",
"name": "name 1",
"thsub": [
{
"id": "11",
"name": "sub 1",
"stats": {
"items": 5,
},
"ions": null
},
{
"id": "22",
"name": "sub 2",
"stats": {
"items": 5,
},
"translations": null
}
],
"image": null
},
{
"id": "2",
"name": "name 2",
"thsub": [
{
"id": "33",
"name": "sub 43",
"stats": {
"items": 20,
},
"ions": null
},
{
"id": "44",
"name": "sub 76",
"stats": {
"items": 5,
},
"translations": null
}
],
"image": null
}
]
}
console.log(data.cars.map(function(item){
return item.thsub.reduce(function(a, b) { return a.stats.items + b.stats.items; });
}));

Categories

Resources