Match and extract element between two arrays of objects by _id - javascript

I have 2 arrays:
Array One:
[
{
"value": {
"_id": "5ce3f8cc35ad1e0999ee18d1",
"is_default": false,
"is_required": true,
"sort_order": 0,
"value": "",
"label": "No 38",
"option_id": "5ce3f8cc35ad1e0999ee18d0",
"__v": 0,
"selected": true
}
},
{
"value": {
"label": "Τρικολορ",
"sort_order": 0,
"value": "#7a3131|#0e8e76|#b6edd9",
"_id": "3aa5b2d7-cb78-44ce-bb5d-e4d42ebf3309",
"selected": true
}
},
{
"value": {
"label": "ΧΛ",
"sort_order": 0,
"value": "",
"_id": "5df37c50854df50b274d7829",
"selected": true
}
}
]
Array Two:
[
{
"_id": "5df384edba99411550e4e019",
"options": [
{
"option": {
"sort_order": 0,
"display_name": "Μέγεθος (EU)",
"type": "text",
"display_style": "dropdown"
},
"value": {
"_id": "5ce3f8cc35ad1e0999ee18d1",
"is_default": false,
"is_required": true,
"sort_order": 0,
"value": "",
"label": "No 38",
"option_id": "5ce3f8cc35ad1e0999ee18d0",
"__v": 0
}
},
{
"option": {
"display_name": "swatch",
"display_style": "swatch",
"sort_order": 0,
"type": "swatch"
},
"value": {
"label": "Τρικολορ",
"sort_order": 0,
"value": "#7a3131|#0e8e76|#b6edd9",
"_id": "3aa5b2d7-cb78-44ce-bb5d-e4d42ebf3309"
}
},
{
"option": {
"display_name": "dropdown",
"display_style": "dropdown",
"sort_order": 0,
"type": "multiplechoice"
},
"value": {
"_id": "5df37c61854df50b274d782a",
"is_default": false,
"is_required": true,
"label": "Λ",
"sort_order": 0,
"value": "",
"value_data": null
}
}
]
},
{
"_id": "5ce3f8cc35ad1e0999ee18d1",
"options": [
{
"option": {
"sort_order": 0,
"display_name": "Μέγεθος (EU)",
"type": "text",
"display_style": "dropdown"
},
"value": {
"_id": "5ce3f8cc35ad1e0999ee18d1",
"is_default": false,
"is_required": true,
"sort_order": 0,
"value": "",
"label": "No 38",
"option_id": "5ce3f8cc35ad1e0999ee18d0",
"__v": 0
}
},
{
"option": {
"display_name": "swatch",
"display_style": "swatch",
"sort_order": 0,
"type": "swatch"
},
"value": {
"label": "Τρικολορ",
"sort_order": 0,
"value": "#7a3131|#0e8e76|#b6edd9",
"_id": "3aa5b2d7-cb78-44ce-bb5d-e4d42ebf3309"
}
},
{
"option": {
"display_name": "dropdown",
"display_style": "dropdown",
"sort_order": 0,
"type": "multiplechoice"
},
"value": {
"label": "ΧΛ",
"sort_order": 0,
"value": "",
"_id": "5df37c50854df50b274d7829"
}
}
]
}
]
As you can see, each value _id from element at index 1 in Array Two matches each value _id from Array One.
How can I iterate Array Two and extract the _id if all elements value _id's matches Array One?
I have tried so many approaches but none seems to work, e.x.:
const opts = newTempProduct.variants.map((item) => item.options);
const props = ['_id'];
const result = opts
.filter((o1, i) => {
o1.filter((o3) => {
return tmpVariant.some((o2) => {
return o3.value._id === o2.value._id;
});
});
})
.map(function(o) {
return props.reduce((newo, name) => {
newo[name] = o[name];
return newo;
}, {});
});

You can create an additional array called search. This array is a mapped version of your first array, to only include the _id property. For efficiency purposes, you can make this array a Set so you can have O(1) lookup. You can then use .every() with .filter() to return true if all ids in the options array for a given object are in search array. This will give you your resulting id:
const arr = [{ "value": { "_id": "5ce3f8cc35ad1e0999ee18d1", "is_default": false, "is_required": true, "sort_order": 0, "value": "", "label": "No 38", "option_id": "5ce3f8cc35ad1e0999ee18d0", "__v": 0, "selected": true } }, { "value": { "label": "Τρικολορ", "sort_order": 0, "value": "#7a3131|#0e8e76|#b6edd9", "_id": "3aa5b2d7-cb78-44ce-bb5d-e4d42ebf3309", "selected": true } }, { "value": { "label": "ΧΛ", "sort_order": 0, "value": "", "_id": "5df37c50854df50b274d7829", "selected": true } } ];
const data = [{ "_id": "5df384edba99411550e4e019", "options": [{ "option": { "sort_order": 0, "display_name": "Μέγεθος (EU)", "type": "text", "display_style": "dropdown" }, "value": { "_id": "5ce3f8cc35ad1e0999ee18d1", "is_default": false, "is_required": true, "sort_order": 0, "value": "", "label": "No 38", "option_id": "5ce3f8cc35ad1e0999ee18d0", "__v": 0 } }, { "option": { "display_name": "swatch", "display_style": "swatch", "sort_order": 0, "type": "swatch" }, "value": { "label": "Τρικολορ", "sort_order": 0, "value": "#7a3131|#0e8e76|#b6edd9", "_id": "3aa5b2d7-cb78-44ce-bb5d-e4d42ebf3309" } }, { "option": { "display_name": "dropdown", "display_style": "dropdown", "sort_order": 0, "type": "multiplechoice" }, "value": { "_id": "5df37c61854df50b274d782a", "is_default": false, "is_required": true, "label": "Λ", "sort_order": 0, "value": "", "value_data": null } } ] }, { "_id": "5ce3f8cc35ad1e0999ee18d1", "options": [{ "option": { "sort_order": 0, "display_name": "Μέγεθος (EU)", "type": "text", "display_style": "dropdown" }, "value": { "_id": "5ce3f8cc35ad1e0999ee18d1", "is_default": false, "is_required": true, "sort_order": 0, "value": "", "label": "No 38", "option_id": "5ce3f8cc35ad1e0999ee18d0", "__v": 0 } }, { "option": { "display_name": "swatch", "display_style": "swatch", "sort_order": 0, "type": "swatch" }, "value": { "label": "Τρικολορ", "sort_order": 0, "value": "#7a3131|#0e8e76|#b6edd9", "_id": "3aa5b2d7-cb78-44ce-bb5d-e4d42ebf3309" } }, { "option": { "display_name": "dropdown", "display_style": "dropdown", "sort_order": 0, "type": "multiplechoice" }, "value": { "label": "ΧΛ", "sort_order": 0, "value": "", "_id": "5df37c50854df50b274d7829" } } ] } ];
const search = new Set(arr.map(({value: {_id}}) => _id));
const res = data.filter(
({options}) => options.every(({value: {_id}}) => search.has(_id))
).map(({_id}) => _id); // use `.filter()` incase many objects match ur first array
console.log(res); // array of all _ids matched
console.log(res.pop()); // your expected/wanted result

Related

TypeError: undefined is not a function (near '...productsCtg.map...') react native

I tried to display products to a screen and what i get is that error,here is my code:
{productsCtg.length > 0 ? (
<View style={styles.listContainer}>
{productsCtg.map((item) => {
console.log(item);
return(
<ProductList
navigation={props.navigation}
key={item.id}
item={item}
/>
);
})}
</View>
) : (
<View style={[styles.center, { height: height / 2}]}>
<Text style={{fontFamily: 'nunito_semi_bold'}}>No products found</Text>
</View>
)}
And i fetch data from an api using useFocusEffect and useCallBack and axios,when app starts products shows but when i try to save any file in the project without making any changes that error appears and when i save again products show up again, i need help and here is another codes:
const [products, setProducts] = useState([]);
const [productsFiltered, setProductsFiltered] = useState([]);
const [focus, setFocus] = useState();
const [categories, setCategories] = useState([]);
const [productsCtg, setProductsCtg] = useState([]);
const [active, setActive] = useState();
const [initialState, setInitialState] = useState([]);
const [loading, setLoading] = useState(true)
useFocusEffect((
useCallback(
() => {
setFocus(false);
setActive(-1);
// Products
axios
.get(`${baseURL}products`)
.then((res) => {
setProducts(res.data);
setProductsFiltered(res.data);
setProductsCtg(res.data);
setInitialState(res.data);
setLoading(false);
//console.log(res.data)
})
.catch((error) => {
console.log('Api call error')
})
//Categories
axios
.get(`${baseURL}categories`)
.then((res) => {
setCategories(res.data);
//console.log(res.data);
})
.catch((error) => {
console.log('Api call error')
})
return () => {
setProducts([]);
setProductsFiltered([]);
setFocus();
setCategories([]);
setActive();
setInitialState();
// setProductsCtg([]);
};
},
[],
)
))
when i console.log before return() the first five arrays are empty and the rest contains repeated data, each array contains six products(objects),and and when i console.log inside a map function all the arrays have repeated data like below(but all),the output of console.log is:
Array []
Array []
Array []
Array []
Array []
Array [
Object {
"brand": "Arusha",
"category": Object {
"__v": 0,
"_id": "62925a7db9e8b1361367c733",
"color": null,
"icon": null,
"name": "food crops",
},
"countInStock": 45,
"dateCreated": "2022-06-04T06:32:06.300Z",
"description": "Mtama mzuri sana kutoka arusha",
"id": "629afc66e400878b161e437b",
"image": "http://2fa0-197-250-230-
109.eu.ngrok.io/public/uploads/21fe5b14-3eee-4218-b554-9a3d17bd801b.jpg-
1654333522641.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Mtama",
"numReviews": 0,
"price": 42000,
"rating": 0,
"richDescription": "undefined",
},
Object {
"brand": "Dodoma",
"category": Object {
"__v": 0,
"_id": "62925a7db9e8b1361367c733",
"color": null,
"icon": null,
"name": "food crops",
},
"countInStock": 27,
"dateCreated": "2022-06-04T08:59:36.678Z",
"description": "Good quality maize in Dodoma,place your order now.",
"id": "629b1ef89e9b9401aba2ba54",
"image": "http://2fa0-197-250-230-
109.eu.ngrok.io/public/uploads/fffe96ff-4558-42db-a6b4-7b5b289995be.jpg-
1654333175940.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Maize",
"numReviews": 0,
"price": 76000,
"rating": 0,
"richDescription": "undefined",
},
Object {
"brand": "Mbeya",
"category": Object {
"__v": 0,
"_id": "62925a7db9e8b1361367c733",
"color": null,
"icon": null,
"name": "food crops",
},
"countInStock": 16,
"dateCreated": "2022-06-05T10:04:19.040Z",
"description": "Mchele mzuri kutoka mbeya,weka oda yako sasa",
"id": "629c7fa3342fe22635019648",
"image": "http://1063-197-250-224-
205.eu.ngrok.io/public/uploads/8c107f75-31e0-4a6c-9835-27a8f6ada063.jpg-
1654423458901.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Rice",
"numReviews": 0,
"price": 80000,
"rating": 0,
"richDescription": "undefined",
},
Object {
"brand": "Tanga",
"category": Object {
"__v": 0,
"_id": "62925a7db9e8b1361367c733",
"color": null,
"icon": null,
"name": "food crops",
},
"countInStock": 12,
"dateCreated": "2022-06-05T10:39:52.196Z",
"description": "Quality beans available,place your order anytime",
"id": "629c87f8499c526723a96979",
"image": "http://1063-197-250-224-
205.eu.ngrok.io/public/uploads/291ee0f1-369e-45a1-8b43-38756c0d866b.jpg-
1654425591386.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Beans",
"numReviews": 0,
"price": 85000,
"rating": 0,
"richDescription": "undefined",
},
Object {
"brand": "Kilimanjaro",
"category": Object {
"__v": 0,
"_id": "62925a7db9e8b1361367c733",
"color": null,
"icon": null,
"name": "food crops",
},
"countInStock": 12,
"dateCreated": "2022-06-05T10:43:04.778Z",
"description": "Karibu upate ndizi kubwa na zenye ubora wa hali ya
juu,weka oda yako sasa",
"id": "629c88b8499c526723a9698c",
"image": "http://1063-197-250-224-
205.eu.ngrok.io/public/uploads/4e10f2a7-9ee0-4735-bd8b-75b1af2d48e4.jpg-
1654425784444.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Ndizi",
"numReviews": 0,
"price": 31500,
"rating": 0,
"richDescription": "undefined",
},
Object {
"brand": "Dar es salaam",
"category": Object {
"__v": 0,
"_id": "62925a7db9e8b1361367c733",
"color": null,
"icon": null,
"name": "food crops",
},
"countInStock": 133,
"dateCreated": "2022-06-05T10:47:48.391Z",
"description": "Tunauza mahindi kwa jumla,weka oda yako mapema,karibu
sana",
"id": "629c89d4499c526723a969a5",
"image": "http://1063-197-250-224-
205.eu.ngrok.io/public/uploads/6895e399-6254-4c46-a757-2d10a0b188b2.jpg-
1654426067682.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Maize",
"numReviews": 0,
"price": 73500,
"rating": 0,
"richDescription": "undefined",
},
]
Array [
Object {
"brand": "Arusha",
"category": Object {
"__v": 0,
"_id": "62925a7db9e8b1361367c733",
"color": null,
"icon": null,
"name": "food crops",
},
"countInStock": 45,
"dateCreated": "2022-06-04T06:32:06.300Z",
"description": "Mtama mzuri sana kutoka arusha",
"id": "629afc66e400878b161e437b",
"image": "http://2fa0-197-250-230-
109.eu.ngrok.io/public/uploads/21fe5b14-3eee-4218-b554-9a3d17bd801b.jpg-
1654333522641.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Mtama",
"numReviews": 0,
"price": 42000,
"rating": 0,
"richDescription": "undefined",
},
Object {
"brand": "Dodoma",
"category": Object {
"__v": 0,
"_id": "62925a7db9e8b1361367c733",
"color": null,
"icon": null,
"name": "food crops",
},
"countInStock": 27,
"dateCreated": "2022-06-04T08:59:36.678Z",
"description": "Good quality maize in Dodoma,place your order now.",
"id": "629b1ef89e9b9401aba2ba54",
"image": "http://2fa0-197-250-230-
109.eu.ngrok.io/public/uploads/fffe96ff-4558-42db-a6b4-7b5b289995be.jpg-
1654333175940.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Maize",
"numReviews": 0,
"price": 76000,
"rating": 0,
"richDescription": "undefined",
},
Object {
"brand": "Mbeya",
"category": Object {
"__v": 0,
"_id": "62925a7db9e8b1361367c733",
"color": null,
"icon": null,
"name": "food crops",
},
"countInStock": 16,
"dateCreated": "2022-06-05T10:04:19.040Z",
"description": "Mchele mzuri kutoka mbeya,weka oda yako sasa",
"id": "629c7fa3342fe22635019648",
"image": "http://1063-197-250-224-
205.eu.ngrok.io/public/uploads/8c107f75-31e0-4a6c-9835-27a8f6ada063.jpg-
1654423458901.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Rice",
"numReviews": 0,
"price": 80000,
"rating": 0,
"richDescription": "undefined",
},
Object {
"brand": "Tanga",
"category": Object {
"__v": 0,
"_id": "62925a7db9e8b1361367c733",
"color": null,
"icon": null,
"name": "food crops",
},
"countInStock": 12,
"dateCreated": "2022-06-05T10:39:52.196Z",
"description": "Quality beans available,place your order anytime",
"id": "629c87f8499c526723a96979",
"image": "http://1063-197-250-224-
205.eu.ngrok.io/public/uploads/291ee0f1-369e-45a1-8b43-38756c0d866b.jpg-
1654425591386.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Beans",
"numReviews": 0,
"price": 85000,
"rating": 0,
"richDescription": "undefined",
},
Object {
"brand": "Kilimanjaro",
"category": Object {
"__v": 0,
"_id": "62925a7db9e8b1361367c733",
"color": null,
"icon": null,
"name": "food crops",
},
"countInStock": 12,
"dateCreated": "2022-06-05T10:43:04.778Z",
"description": "Karibu upate ndizi kubwa na zenye ubora wa hali ya
juu,weka oda yako sasa",
"id": "629c88b8499c526723a9698c",
"image": "http://1063-197-250-224-
205.eu.ngrok.io/public/uploads/4e10f2a7-9ee0-4735-bd8b-75b1af2d48e4.jpg-
1654425784444.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Ndizi",
"numReviews": 0,
"price": 31500,
"rating": 0,
"richDescription": "undefined",
},
Object {
"brand": "Dar es salaam",
"category": Object {
"__v": 0,
"_id": "62925a7db9e8b1361367c733",
"color": null,
"icon": null,
"name": "food crops",
},
"countInStock": 133,
"dateCreated": "2022-06-05T10:47:48.391Z",
"description": "Tunauza mahindi kwa jumla,weka oda yako mapema,karibu
sana",
"id": "629c89d4499c526723a969a5",
"image": "http://1063-197-250-224-
205.eu.ngrok.io/public/uploads/6895e399-6254-4c46-a757-2d10a0b188b2.jpg-
1654426067682.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Maize",
"numReviews": 0,
"price": 73500,
"rating": 0,
"richDescription": "undefined",
},
]
Array [
Object {
"brand": "Arusha",
"category": Object {
"__v": 0,
"_id": "62925a7db9e8b1361367c733",
"color": null,
"icon": null,
"name": "food crops",
},
"countInStock": 45,
"dateCreated": "2022-06-04T06:32:06.300Z",
"description": "Mtama mzuri sana kutoka arusha",
"id": "629afc66e400878b161e437b",
"image": "http://2fa0-197-250-230-
109.eu.ngrok.io/public/uploads/21fe5b14-3eee-4218-b554-9a3d17bd801b.jpg-
1654333522641.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Mtama",
"numReviews": 0,
"price": 42000,
"rating": 0,
"richDescription": "undefined",
},
Object {
"brand": "Dodoma",
"category": Object {
"__v": 0,
"_id": "62925a7db9e8b1361367c733",
"color": null,
"icon": null,
"name": "food crops",
},
"countInStock": 27,
"dateCreated": "2022-06-04T08:59:36.678Z",
"description": "Good quality maize in Dodoma,place your order now.",
"id": "629b1ef89e9b9401aba2ba54",
"image": "http://2fa0-197-250-230-
109.eu.ngrok.io/public/uploads/fffe96ff-4558-42db-a6b4-7b5b289995be.jpg-
1654333175940.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Maize",
"numReviews": 0,
"price": 76000,
"rating": 0,
"richDescription": "undefined",
},
Object {
"brand": "Mbeya",
"category": Object {
"__v": 0,
"_id": "62925a7db9e8b1361367c733",
"color": null,
"icon": null,
"name": "food crops",
},
"countInStock": 16,
"dateCreated": "2022-06-05T10:04:19.040Z",
"description": "Mchele mzuri kutoka mbeya,weka oda yako sasa",
"id": "629c7fa3342fe22635019648",
"image": "http://1063-197-250-224-
205.eu.ngrok.io/public/uploads/8c107f75-31e0-4a6c-9835-27a8f6ada063.jpg-
1654423458901.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Rice",
"numReviews": 0,
"price": 80000,
"rating": 0,
"richDescription": "undefined",
},
Object {
"brand": "Tanga",
"category": Object {
"__v": 0,
"_id": "62925a7db9e8b1361367c733",
"color": null,
"icon": null,
"name": "food crops",
},
"countInStock": 12,
"dateCreated": "2022-06-05T10:39:52.196Z",
"description": "Quality beans available,place your order anytime",
"id": "629c87f8499c526723a96979",
"image": "http://1063-197-250-224-
205.eu.ngrok.io/public/uploads/291ee0f1-369e-45a1-8b43-38756c0d866b.jpg-
1654425591386.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Beans",
"numReviews": 0,
"price": 85000,
"rating": 0,
"richDescription": "undefined",
},
Object {
"brand": "Kilimanjaro",
"category": Object {
"__v": 0,
"_id": "62925a7db9e8b1361367c733",
"color": null,
"icon": null,
"name": "food crops",
},
"countInStock": 12,
"dateCreated": "2022-06-05T10:43:04.778Z",
"description": "Karibu upate ndizi kubwa na zenye ubora wa hali ya
juu,weka oda yako sasa",
"id": "629c88b8499c526723a9698c",
"image": "http://1063-197-250-224-
205.eu.ngrok.io/public/uploads/4e10f2a7-9ee0-4735-bd8b-75b1af2d48e4.jpg-
1654425784444.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Ndizi",
"numReviews": 0,
"price": 31500,
"rating": 0,
"richDescription": "undefined",
},
Object {
"brand": "Dar es salaam",
"category": Object {
"__v": 0,
"_id": "62925a7db9e8b1361367c733",
"color": null,
"icon": null,
"name": "food crops",
},
"countInStock": 133,
"dateCreated": "2022-06-05T10:47:48.391Z",
"description": "Tunauza mahindi kwa jumla,weka oda yako mapema,karibu
sana",
"id": "629c89d4499c526723a969a5",
"image": "http://1063-197-250-224-
205.eu.ngrok.io/public/uploads/6895e399-6254-4c46-a757-2d10a0b188b2.jpg-
1654426067682.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Maize",
"numReviews": 0,
"price": 73500,
"rating": 0,
"richDescription": "undefined",
},
]
check type of productsCtg if it is Object;
This error means productsCtg is not a array
Try:
console.log(productsCtg)

Why is this not iterable?

Hello I am writing an angular app. I have a Node API returning data which I am trying to chart using Chart.js. I am getting my data from the API and it returns an object like this:
{
"0": {
"_id": "62716061f6897b637f59ba9e",
"created": "2022-05-03T17:03:29.459Z",
"items": [
{
"name": "Air Temperature",
"imperial": 66.42499542,
"metric": 19.0625,
"controlStatus": false,
"_id": "62716061f6897b637f59ba9f"
},
{
"name": "Outside Air Temperature",
"imperial": 65.75,
"metric": 18.6875,
"controlStatus": false,
"_id": "62716061f6897b637f59baa0"
},
{
"name": "Water Temperature",
"imperial": 65.75,
"metric": 18.75,
"controlStatus": false,
"_id": "62716061f6897b637f59baa1"
}
],
"settings": [
{
"name": "in1",
"onValue": 80,
"offValue": 78,
"status": "",
"unitType": "Temp",
"_id": "62716061f6897b637f59baa2"
},
{
"name": "in2",
"onValue": 0,
"offValue": 0,
"status": "",
"unitType": "Temp",
"_id": "62716061f6897b637f59baa3"
},
{
"name": "in3",
"onValue": 0,
"offValue": 0,
"status": "",
"unitType": "Temp",
"_id": "62716061f6897b637f59baa4"
},
{
"name": "in4",
"onValue": 0,
"offValue": 0,
"status": "",
"unitType": "Temp",
"_id": "62716061f6897b637f59baa5"
}
],
"__v": 0
},
"1": {
"_id": "62715f33f6897b637f59ba92",
"created": "2022-05-03T16:58:27.064Z",
"items": [
{
"name": "Air Temperature",
"imperial": 66.3125,
"metric": 19.0625,
"controlStatus": false,
"_id": "62715f33f6897b637f59ba93"
},
{
"name": "Outside Air Temperature",
"imperial": 65.63749695,
"metric": 18.6875,
"controlStatus": false,
"_id": "62715f33f6897b637f59ba94"
},
{
"name": "Water Temperature",
"imperial": 65.75,
"metric": 18.75,
"controlStatus": false,
"_id": "62715f33f6897b637f59ba95"
}
],
"settings": [
{
"name": "in1",
"onValue": 80,
"offValue": 78,
"status": "",
"unitType": "Temp",
"_id": "62715f33f6897b637f59ba96"
},
{
"name": "in2",
"onValue": 0,
"offValue": 0,
"status": "",
"unitType": "Temp",
"_id": "62715f33f6897b637f59ba97"
},
{
"name": "in3",
"onValue": 0,
"offValue": 0,
"status": "",
"unitType": "Temp",
"_id": "62715f33f6897b637f59ba98"
},
{
"name": "in4",
"onValue": 0,
"offValue": 0,
"status": "",
"unitType": "Temp",
"_id": "62715f33f6897b637f59ba99"
}
],
"__v": 0
},
"2": {
"_id": "62715e04f6897b637f59ba72",
"created": "2022-05-03T16:53:24.706Z",
"items": [
{
"name": "Air Temperature",
"imperial": 66.19999695,
"metric": 19.0625,
"controlStatus": false,
"_id": "62715e04f6897b637f59ba73"
},
{
"name": "Outside Air Temperature",
"imperial": 65.63749695,
"metric": 18.6875,
"controlStatus": false,
"_id": "62715e04f6897b637f59ba74"
},
{
"name": "Water Temperature",
"imperial": 65.75,
"metric": 18.75,
"controlStatus": false,
"_id": "62715e04f6897b637f59ba75"
}
],
"settings": [
{
"name": "in1",
"onValue": 80,
"offValue": 78,
"status": "",
"unitType": "Temp",
"_id": "62715e04f6897b637f59ba76"
},
{
"name": "in2",
"onValue": 0,
"offValue": 0,
"status": "",
"unitType": "Temp",
"_id": "62715e04f6897b637f59ba77"
},
{
"name": "in3",
"onValue": 0,
"offValue": 0,
"status": "",
"unitType": "Temp",
"_id": "62715e04f6897b637f59ba78"
},
{
"name": "in4",
"onValue": 0,
"offValue": 0,
"status": "",
"unitType": "Temp",
"_id": "62715e04f6897b637f59ba79"
}
],
"__v": 0
},
"3": {
"_id": "62715cd6f6897b637f59ba66",
"created": "2022-05-03T16:48:22.086Z",
"items": [
{
"name": "Air Temperature",
"imperial": 66.19999695,
"metric": 19,
"controlStatus": false,
"_id": "62715cd6f6897b637f59ba67"
},
{
"name": "Outside Air Temperature",
"imperial": 65.63749695,
"metric": 18.6875,
"controlStatus": false,
"_id": "62715cd6f6897b637f59ba68"
},
{
"name": "Water Temperature",
"imperial": 65.75,
"metric": 18.75,
"controlStatus": false,
"_id": "62715cd6f6897b637f59ba69"
}
],
"settings": [
{
"name": "in1",
"onValue": 80,
"offValue": 78,
"status": "",
"unitType": "Temp",
"_id": "62715cd6f6897b637f59ba6a"
},
{
"name": "in2",
"onValue": 0,
"offValue": 0,
"status": "",
"unitType": "Temp",
"_id": "62715cd6f6897b637f59ba6b"
},
{
"name": "in3",
"onValue": 0,
"offValue": 0,
"status": "",
"unitType": "Temp",
"_id": "62715cd6f6897b637f59ba6c"
},
{
"name": "in4",
"onValue": 0,
"offValue": 0,
"status": "",
"unitType": "Temp",
"_id": "62715cd6f6897b637f59ba6d"
}
],
"__v": 0
}
}
I am trying to iterate over this to parse my data points but I am getting errors. Here is my code:
constructDataSet(input: IGreenhouseData[]) {
let labels: string[] = [];
let imperial: number[] = [];
let metric: number[] = [];
let datasets: IDataSet[] = [];
for (let item of input) {
for (let dataPoint of item.items) {
labels.push(dataPoint.name);
imperial.push(dataPoint.imperial);
metric.push(dataPoint.metric);
}
let newDataSet: IDataSet = {
label: 'Imperial',
data: imperial,
};
datasets.push(newDataSet);
newDataSet = {
label: 'Metric',
data: metric,
};
datasets.push(newDataSet);
}
this.data = {
labels: labels,
datasets: datasets,
};
}
This is the error:
ERROR TypeError: input is not iterable
input is not an array, but an object. Change the for-loop to:
for (let item of Object.values(input)) {
/*rest of the code*/
}
so the iteration is done over the object values rather than the object itself.

More efficient way to remove from an object attributes present in a group of objects

I'm manipulating some javascript objects and I want to know if is there a more efficient and easy way to process my data.
I already do that, but I'm a beginner in js.
I have four objects with this structure: basically there is an array of blocks and any object has a different number of blocks. In every block, in the features attribute, I have another array with some features.
Then I have another object, and I have to remove from this object (I call it structure) blocks and features that are not present in my four initial object.
This is a sample product object
[
{
"ID": 16293,
"SortNo": "20",
"FeatureGroup": {
"ID": "148",
"Name": {
"Value": "Design",
"Language": "IT"
}
},
"Features": [
{
"Localized": 0,
"ID": "155744521",
"Type": "dropdown",
"Value": "Round",
"CategoryFeatureId": "85327",
"CategoryFeatureGroupID": "16293",
"SortNo": "155",
"PresentationValue": "Rotondo",
"RawValue": "Round",
"LocalValue": [],
"Description": "The external form",
"Mandatory": "1",
"Searchable": "0",
"Feature": {
"ID": "9397",
"Sign": "",
"Measure": {
"ID": "29",
"Sign": "",
"Signs": {
"ID": "",
"_": "",
"Language": "IT"
}
},
"Name": {
"Value": "Forma",
"Language": "IT"
}
}
},
{
"Localized": 0,
"ID": "155655523",
"Type": "multi_dropdown",
"Value": "White",
"CategoryFeatureId": "85298",
"CategoryFeatureGroupID": "16293",
"SortNo": "90",
"PresentationValue": "Bianco",
"RawValue": "White",
"LocalValue": [],
"Description": "The colour of the housing",
"Mandatory": "1",
"Searchable": "1",
"Feature": {
"ID": "10059",
"Sign": "",
"Measure": {
"ID": "29",
"Sign": "",
"Signs": {
"ID": "",
"_": "",
"Language": "IT"
}
},
"Name": {
"Value": "Colore struttura",
"Language": "IT"
}
}
},
{
"Localized": 0,
"ID": "155655525",
"Type": "multi_dropdown",
"Value": "White",
"CategoryFeatureId": "85301",
"CategoryFeatureGroupID": "16293",
"SortNo": "80",
"PresentationValue": "Bianco",
"RawValue": "White",
"LocalValue": [],
"Description": "The colour of the band",
"Mandatory": "1",
"Searchable": "1",
"Feature": {
"ID": "11025",
"Sign": "",
"Measure": {
"ID": "29",
"Sign": "",
"Signs": {
"ID": "",
"_": "",
"Language": "IT"
}
},
"Name": {
"Value": "Colore cinturino",
"Language": "IT"
}
}
},
{
"Localized": 0,
"ID": "219617494",
"Type": "y_n",
"Value": "Y",
"CategoryFeatureId": "168947",
"CategoryFeatureGroupID": "16293",
"SortNo": "-6",
"PresentationValue": "Sì",
"RawValue": "Y",
"LocalValue": [],
"Description": "The product is protected from water",
"Mandatory": "0",
"Searchable": "0",
"Feature": {
"ID": "7509",
"Sign": "",
"Measure": {
"ID": "26",
"Sign": "",
"Signs": {
"ID": "",
"_": "",
"Language": "IT"
}
},
"Name": {
"Value": "Resistente all'acqua",
"Language": "IT"
}
}
}
]
},
{
"ID": 34567,
"SortNo": "20",
"FeatureGroup": {
"ID": "184",
"Name": {
"Value": "Prestazione",
"Language": "IT"
}
},
"Features": [
{
"Localized": 0,
"ID": "155744528",
"Type": "y_n",
"Value": "N",
"CategoryFeatureId": "94697",
"CategoryFeatureGroupID": "34567",
"SortNo": "800",
"PresentationValue": "No",
"RawValue": "N",
"LocalValue": [],
"Description": "La Frequenza modulare radio produce la miglior recezione di qualsiasi canale radio. Quando viene usato un auricolare, produce un effetto di suono da stereo r",
"Mandatory": "1",
"Searchable": "0",
"Feature": {
"ID": "2172",
"Sign": "",
"Measure": {
"ID": "26",
"Sign": "",
"Signs": {
"ID": "",
"_": "",
"Language": "IT"
}
},
"Name": {
"Value": "Radio FM",
"Language": "IT"
}
}
},
{
"Localized": 0,
"ID": "155744530",
"Type": "multi_dropdown",
"Value": "Not supported",
"CategoryFeatureId": "85357",
"CategoryFeatureGroupID": "34567",
"SortNo": "500",
"PresentationValue": "Non supportato",
"RawValue": "Not supported",
"LocalValue": [],
"Description": "Types of memory cards which can be used with this product.",
"Mandatory": "1",
"Searchable": "0",
"Feature": {
"ID": "730",
"Sign": "",
"Measure": {
"ID": "29",
"Sign": "",
"Signs": {
"ID": "",
"_": "",
"Language": "IT"
}
},
"Name": {
"Value": "Tipi schede di memoria",
"Language": "IT"
}
}
}
]
}
]
Here i loop my initial objects (this.compare_products) to extract, in two arrays (featureGroupIds - featureIds) the ID of my block and the CategoryFeatureId
let featureGroupIds = []
let featureIds = []
this.compare_products.forEach((object) => {
featureGroupIds = featureGroupIds.concat(FeaturesGroups.map(o => o.ID))
featureIds = featureIds.concat(FeaturesGroups.map(o => o.Features.map(o => o. CategoryFeatureId))).flat(2)
})
The two arrays, featureGroupIds and featureIds are now filled with every block ID and every CategoryFeatureId present in my four object.
Now I have to filter the object I call "structure" to remove the block and the features with an ID that is not present in my arrays.
This is my structure, and as you can see is similar.
[
{
"name": "Display",
"data": {
"id": 34566,
"category_id": 2647
},
"features": [
{
"name": "Tipo di display",
"data": {
"id": 85325,
"category_id": 2647,
"feature_id": 9104,
"category_feature_group_id": 34566,
"order": 10100140
}
},
{
"name": "Touch screen",
"data": {
"id": 85331,
"category_id": 2647,
"feature_id": 4963,
"category_feature_group_id": 34566,
"order": 10100129
}
},
{
"name": "Dimensioni schermo",
"data": {
"id": 158002,
"category_id": 2647,
"feature_id": 3544,
"category_feature_group_id": 34566,
"order": 100149
}
},
{
"name": "à di Pixel",
"data": {
"id": 85347,
"category_id": 2647,
"feature_id": 13246,
"category_feature_group_id": 34566,
"order": 100147
}
},
{
"name": "Tipo di vetro",
"data": {
"id": 94704,
"category_id": 2647,
"feature_id": 7610,
"category_feature_group_id": 34566,
"order": 100050
}
}
]
},
{
"name": "Altre caratteristiche",
"data": {
"id": 34569,
"category_id": 2647,
"feature_group_id": 146,
"name": null,
"order": 0
},
"features": [
{
"name": "inside",
"data": {
"id": 110410,
"category_id": 2647,
"feature_id": 18688,
"category_feature_group_id": 34569,
"order": 100000
}
}
]
}
]
Here is my function
structure = structure.filter(featureGroup => this.featureGroupIds.includes(featureGroup.data.id));
structure.map((object) => {
object.features.filter(feature => this.featureIds.includes(feature.data.feature_id))
})
this.featureIds and this.featureGroupIds are the array with the group IDS and with the feature IDS.
Is there a more efficient way to do this?

How can i sort array with objects which have property with another array of objects but not with ES6 [duplicate]

This question already has answers here:
Sort nested array of object in javascript
(4 answers)
Closed 3 years ago.
I have this kind of array with objects. In each object except properties i have another array with objects called "secondary_fields". I need to access the last element in the secondary_fields array (secondary_fields[3]) where i have value of when it is created and after that to sort all of the objects
by the date time based on the value inside for example "2020-01-13 17:42:51";
But not with ES6 because i am using old platform where the ES6 version of Java Script is not supported
let arr =
[
{
"external": false,
"link": "--",
"direct": false,
"display_field": "new best article",
"id": "kb_knowledge:33",
"secondary_fields": [
{
"display_value": "Test Admin",
"name": "author",
"label": "Author",
"type": "reference",
"value": "xx"
},
{
"display_value": "25",
"name": "sys_view_count",
"label": "View count",
"type": "integer",
"value": "25"
},
{
"display_value": "2020-01-13 09:44:54",
"name": "sys_updated_on",
"label": "Updated",
"type": "glide_date_time",
"value": "2020-01-13 17:44:54"
},
{
"display_value": "2020-01-13 09:42:51",
"name": "sys_created_on",
"label": "Created",
"type": "glide_date_time",
"value": "2020-01-13 17:42:51"
}
],
},
{
"external": false,
"link": "--",
"direct": false,
"display_field": "How to connect the iPod to Wi-Fi",
"id": "kb_knowledge:11",
"secondary_fields": [
{
"display_value": "John",
"name": "author",
"label": "Author",
"type": "reference",
"value": "xxnnx"
},
{
"display_value": "16",
"name": "sys_view_count",
"label": "View count",
"type": "integer",
"value": "16"
},
{
"display_value": "2019-12-18 08:18:08",
"name": "sys_updated_on",
"label": "Updated",
"type": "glide_date_time",
"value": "2019-12-18 16:18:08"
},
{
"display_value": "2019-10-21 12:27:22",
"name": "sys_created_on",
"label": "Created",
"type": "glide_date_time",
"value": "2019-10-21 19:27:22"
}
],
}
]
You can make use of the array method's sort method and sort on display_value.
arr.sort(function(a, b) {
return new Date(a.secondary_fields[3].display_value) - new Date(b.secondary_fields[3].display_value)
})
Edit
Or I guess in your case,
return new Date(b.secondary_fields[3].display_value) - new Date(a.secondary_fields[3].display_value)
let arr =
[
{
"external": false,
"link": "--",
"direct": false,
"display_field": "new best article",
"id": "kb_knowledge:33",
"secondary_fields": [
{
"display_value": "Test Admin",
"name": "author",
"label": "Author",
"type": "reference",
"value": "xx"
},
{
"display_value": "25",
"name": "sys_view_count",
"label": "View count",
"type": "integer",
"value": "25"
},
{
"display_value": "2020-01-13 09:44:54",
"name": "sys_updated_on",
"label": "Updated",
"type": "glide_date_time",
"value": "2020-01-13 17:44:54"
},
{
"display_value": "2020-01-13 09:42:51",
"name": "sys_created_on",
"label": "Created",
"type": "glide_date_time",
"value": "2020-01-13 17:42:51"
}
],
},
{
"external": false,
"link": "--",
"direct": false,
"display_field": "How to connect the iPod to Wi-Fi",
"id": "kb_knowledge:11",
"secondary_fields": [
{
"display_value": "John",
"name": "author",
"label": "Author",
"type": "reference",
"value": "xxnnx"
},
{
"display_value": "16",
"name": "sys_view_count",
"label": "View count",
"type": "integer",
"value": "16"
},
{
"display_value": "2019-12-18 08:18:08",
"name": "sys_updated_on",
"label": "Updated",
"type": "glide_date_time",
"value": "2019-12-18 16:18:08"
},
{
"display_value": "2019-10-21 12:27:22",
"name": "sys_created_on",
"label": "Created",
"type": "glide_date_time",
"value": "2019-10-21 19:27:22"
}
],
}
]
arr.sort(function(a, b){
return new Date(b.secondary_fields[3].display_value) - new Date(a.secondary_fields[3].display_value)
})
console.log(arr)

ng-repeat error: "Duplicates in a repeater are not allowed"

Here is my html,
<tr ng-repeat="val in attribute.format.values">
I am getting,
[Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: val in attribute.format.values, Duplicate key: object:07F, Duplicate value: {2}][1]
Here is my JSON,
[{
"name": "Auto",
"weight": "1",
"value": "1",
"count": 0,
"min": 0,
"max": 0,
"children": [],
"id": "06c9f57c-963f-4977-bca5-9226e971a8dc",
"visible": true,
"$$hashKey": "06V"
}, {
"name": "On",
"weight": "1",
"value": "2",
"count": 0,
"min": 0,
"max": 0,
"children": [],
"id": "dc019b87-1da5-4f21-b91f-4ee35ec04eb8",
"visible": true,
"$$hashKey": "06W"
}, {
"name": "Off",
"weight": "1",
"value": "4",
"count": 0,
"min": 0,
"max": 0,
"children": [],
"id": "daf8ef80-a2da-4e02-8960-791b8528905e",
"visible": true,
"$$hashKey": "06X"
}, {
"name": "Redeye Reduction",
"weight": "1",
"value": "5",
"count": 0,
"min": 0,
"max": 0,
"children": [],
"id": "87ccb4ee-39cd-4bd0-9817-7e477af6d5b0",
"visible": true,
"$$hashKey": "06Y"
}, {
"name": "Slow Sync",
"weight": "1",
"value": "6",
"count": 0,
"min": 0,
"max": 0,
"children": [],
"id": "e0b14c8b-af4b-4ab8-a533-32ac4829613f",
"visible": true,
"$$hashKey": "06Z"
}, {
"name": "High Speed Sync",
"weight": "1",
"value": "7",
"count": 0,
"min": 0,
"max": 0,
"children": [],
"id": "622c7ebf-067c-46ed-913a-ba045a0586df",
"visible": true,
"$$hashKey": "070"
}, {
"name": "Front Curtain",
"weight": "1",
"value": "8",
"count": 0,
"min": 0,
"max": 0,
"children": [],
"id": "fe12c21c-e2c3-4702-ae9b-82f2fe248574",
"visible": true,
"$$hashKey": "071"
}, {
"name": "Rear Curtain",
"weight": "1",
"value": "9",
"count": 0,
"min": 0,
"max": 0,
"children": [],
"id": "4a229ebb-d029-4b1e-a13d-246d00215900",
"visible": true,
"$$hashKey": "072"
}, {
"name": "Fill-in",
"weight": "1",
"value": "10",
"count": 0,
"min": 0,
"max": 0,
"children": [],
"id": "a7ed372d-398f-4614-8efa-64a338f0ad20",
"visible": true,
"$$hashKey": "073"
}, {
"name": "Wireless",
"weight": "1",
"value": "11",
"count": 0,
"min": 0,
"max": 0,
"children": [],
"id": "b36ab529-feab-42aa-8863-2a7084ac0aba",
"visible": true,
"$$hashKey": "074"
}, {
"name": "Redeye Reduction with Slow Sync",
"value": "12",
"weight": "1",
"visible": true,
"id": "23ee6ac2-1b8b-41a7-ae80-aa4fcd8134ae",
"$$hashKey": "07F"
}, {
"name": "Slow Rear-Curtain Sync",
"value": "13",
"weight": "1",
"visible": true,
"id": "f5835da6-4eac-4878-a8c0-3f91cd22372f",
"$$hashKey": "07J"
}, {
"name": "Repeating or Strobe",
"value": "14",
"weight": "1",
"visible": true,
"id": "8f3bb252-ae6e-426a-9781-d1232a0a4845",
"$$hashKey": "0CW"
}, {
"name": "Rear Sync",
"value": "15",
"weight": "1",
"visible": true,
"id": "d668aa49-a999-4638-8af2-5e9eaafd6e75",
"$$hashKey": "06L"
}, {
"name": "Fill-Flaw",
"value": "16",
"weight": "1",
"visible": true,
"id": "00f92889-05f9-45fc-b3de-37e224991548",
"$$hashKey": "08D"
}, {
"name": "Advanced Flash",
"value": "17",
"weight": "1",
"visible": true,
"id": "d49c3d25-2bf5-4723-b7d4-c3ae36783097",
"$$hashKey": "079"
}, {
"name": "Fill-Flash",
"value": "18",
"weight": "1",
"visible": true,
"id": "4693db2a-19c7-4839-8e65-875e36416c63",
"$$hashKey": "0ER"
}, {
"name": "Smart Flash",
"value": "19",
"weight": "1",
"visible": true,
"id": "8b04d59b-75cc-46ff-acc0-33f410f64a09",
"$$hashKey": "07D"
}, {
"name": "",
"value": "",
"weight": "",
"visible": true,
"id": "18cec753-48fe-47e6-9e67-1bb36972bee4",
"$$hashKey": "07F"
}, {
"name": "",
"value": "",
"weight": "",
"visible": true,
"id": "a7fbad1d-2a49-40e7-a9b1-520db0f9fc65"
}]
<tr ng-repeat="val in attribute.format.values track by id">
edit like this
id could be the identity of the objects so you can tack though the id property, that will keep one object if you have add two objects that shares same id mistakenly.
Try this:
you can use 'track by $index'
<tr ng-repeat="val in attribute.format.values track by $index">

Categories