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.
Related
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)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
[{
"id": "5f283e239b81681618b19b5d",
"listItemId": 3,
"formData": {
"name": "הבית שלנו בחיפה ",
"address": "הגולן 150",
"geo": "ישראל",
"currency": "NIS",
"productPrice": "150000",
"expectedRevenue": "6955"
},
"productId": 1,
"formId": 1,
"createdAt": "2020-08-03T16:41:07.691Z",
"updatedAt": "2020-08-03T16:42:49.407Z",
"summary": {
"appreciationPer": 0,
"sumYieldPer": 0,
"portfolioPer": 0.022,
"worth": {
"currency": "ILS",
"value": 150000
},
"yieldPer": 0
}
}, {
"id": "5f283e499b81681618b19b5e",
"listItemId": 4,
"formData": {
"name": "הבית שלנו בארהבב",
"address": "רבי מלוביץת 10",
"geo": "ארהב",
"currency": "USD",
"productPrice": "1500000",
"expectedRevenue": "699"
},
"productId": 1,
"formId": 1,
"createdAt": "2020-08-03T16:41:45.317Z",
"updatedAt": "2020-08-03T16:41:45.317Z",
"summary": {
"appreciationPer": 0,
"sumYieldPer": 0,
"portfolioPer": 0.761,
"worth": {
"currency": "ILS",
"value": 5261700
},
"yieldPer": 0
}
}, {
"id": "5f283e699b81681618b19b5f",
"listItemId": 5,
"formData": {
"name": "דירהב אירופה",
"address": "אירוהפם 2",
"geo": "אירופה",
"currency": "NIS",
"productPrice": "1500000",
"expectedRevenue": "6000"
},
"productId": 1,
"formId": 1,
"createdAt": "2020-08-03T16:42:17.087Z",
"updatedAt": "2020-08-03T16:42:17.087Z",
"summary": {
"appreciationPer": 0,
"sumYieldPer": 0,
"portfolioPer": 0.761,
"worth": {
"currency": "ILS",
"value": 5261700
},
"yieldPer": 0
}
}]
hey, how to Map this array to get a calculated sum of all the summry.worth.value property in side each object...
can someone pls help me figure how to do it?
Use Array.prototype.reduce().
The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in single output value.
var total = array.reduce((accumulator, current_value)=> {
return accumulator + current_value.summary.worth.value;
}, 0);
Example:
var array = [{
"id": "5f283e239b81681618b19b5d",
"listItemId": 3,
"formData": {
"name": "הבית שלנו בחיפה ",
"address": "הגולן 150",
"geo": "ישראל",
"currency": "NIS",
"productPrice": "150000",
"expectedRevenue": "6955"
},
"productId": 1,
"formId": 1,
"createdAt": "2020-08-03T16:41:07.691Z",
"updatedAt": "2020-08-03T16:42:49.407Z",
"summary": {
"appreciationPer": 0,
"sumYieldPer": 0,
"portfolioPer": 0.022,
"worth": {
"currency": "ILS",
"value": 150000
},
"yieldPer": 0
}
},
{
"id": "5f283e499b81681618b19b5e",
"listItemId": 4,
"formData": {
"name": "הבית שלנו בארהבב",
"address": "רבי מלוביץת 10",
"geo": "ארהב",
"currency": "USD",
"productPrice": "1500000",
"expectedRevenue": "699"
},
"productId": 1,
"formId": 1,
"createdAt": "2020-08-03T16:41:45.317Z",
"updatedAt": "2020-08-03T16:41:45.317Z",
"summary": {
"appreciationPer": 0,
"sumYieldPer": 0,
"portfolioPer": 0.761,
"worth": {
"currency": "ILS",
"value": 5261700
},
"yieldPer": 0
}
},
{
"id": "5f283e699b81681618b19b5f",
"listItemId": 5,
"formData": {
"name": "דירהב אירופה",
"address": "אירוהפם 2",
"geo": "אירופה",
"currency": "NIS",
"productPrice": "1500000",
"expectedRevenue": "6000"
},
"productId": 1,
"formId": 1,
"createdAt": "2020-08-03T16:42:17.087Z",
"updatedAt": "2020-08-03T16:42:17.087Z",
"summary": {
"appreciationPer": 0,
"sumYieldPer": 0,
"portfolioPer": 0.761,
"worth": {
"currency": "ILS",
"value": 5261700
},
"yieldPer": 0
}
}
];
var total = array.reduce((accumulator, current)=> accumulator+current.summary.worth.value, 0);
console.log(total);
I'm filtering a list using a const queryModifier = {price: "lessThan", weight: "greaterThan"}
const queryKeys = keys: {
price: '1000',
weight: '1000'
}
const list = [
{
"clientOrderNumber": "N / A",
"companyName": "Test Company",
"createdAt": "2019-11-05 10:48:18",
"createdBy": "test#test.com",
"deliveryDate": "2019-11-08 10:46:37",
"driver": "",
"dropOff": "Kögel Trailer GmbH & Co. KG, Am Kögel-Werk, Burtenbach, Germany",
"height": 0,
"isPreparingTrailer": false,
"isSmsSent": false,
"isTrailerReady": false,
"key": "7e249529-d089-47bb-b0ad-470f850dd8cf",
"notes": "",
"orderId": "",
"pickUp": "Altenberge, Germany",
"price": 500,
"quantity": 1,
"trailer": "einzeln",
"vehicle": "Sattel",
"vehicleClass": "engl. Anschlüsse",
"vehicleId": "505123",
"vehicleReadyDate": "2019-11-05 10:47:57",
"weight": 0
},
{
"additionalPrices": 0,
"clientOrderNumber": "N / A",
"companyName": "Test Company",
"createdAt": "2019-10-14 16:32:12",
"createdBy": "test#test.com",
"deliveryDate": "2019-10-19 16:26:35",
"driver": "",
"dropOff": "Marville-Moutiers-Brûlé, France",
"height": 600,
"isPreparingTrailer": false,
"isSmsSent": false,
"isTrailerReady": false,
"key": "9b7f57fd-f95e-4038-b120-a0301fdf3f31",
"notes": "",
"orderId": "",
"pickUp": "Altenberge, Germany",
"price": 0,
"quantity": 1,
"trailer": "2er Pack",
"vehicle": "Anhänger",
"vehicleClass": "50",
"vehicleId": "123",
"vehicleReadyDate": "2019-10-16 16:31:26",
"weight": 12000
},
{
"clientOrderNumber": "N / A",
"companyName": "Test Company",
"createdAt": "2019-10-14 16:25:54",
"createdBy": "test#test.com",
"deliveryDate": "2019-10-19 16:24:13",
"driver": "",
"dropOff": "Vendenheim, France",
"height": 0,
"isPreparingTrailer": false,
"isSmsSent": false,
"isTrailerReady": false,
"key": "7e4f233f-695b-40eb-a0ca-fd78f3fa43cd",
"notes": "",
"orderId": "",
"pickUp": "Altenberge, Germany",
"price": 793,
"quantity": 1,
"trailer": "einzeln",
"vehicle": "Sattel",
"vehicleClass": "Standard",
"vehicleId": "3340731",
"vehicleReadyDate": "2019-10-15 16:24:58",
"weight": 0
},
{
"clientOrderNumber": "N / A",
"companyName": "Test Company",
"createdAt": "2019-09-26 18:32:18",
"createdBy": "test#test.com",
"deliveryDate": "2019-09-20 18:31:45",
"driver": "Michal Kucharski",
"dropOff": "Logroño, Spain",
"height": 0,
"isPreparingTrailer": false,
"isSmsSent": false,
"isTrailerReady": false,
"key": "388113f5-3927-4fe3-80d5-f2fcf1c7cedd",
"notes": "",
"orderId": "",
"pickUp": "16671 Butano Place, Fontana, CA, USA",
"price": 0,
"quantity": 1,
"trailer": "2er Pack",
"vehicle": "Sattel",
"vehicleClass": "Standard",
"vehicleId": "efgefg",
"vehicleReadyDate": "2019-09-27 18:32:08",
"weight": 0
},
{
"additionalPrices": 0,
"clientOrderNumber": "N / A",
"companyName": "Test Company",
"createdAt": "2019-09-06 22:57:55",
"createdBy": "test#test.com",
"deliveryDate": "2019-09-07 22:57:03",
"driver": "Eugeniusz Galinski",
"dropOff": "12345 Lamplight Village Avenue, Austin, TX, USA",
"height": 32,
"isPreparingTrailer": false,
"isSmsSent": false,
"isTrailerReady": false,
"key": "ac3cf14e-b43b-45e4-9168-ad4997b6415d",
"notes": "Nzube I am adding notes here",
"pickUp": "16671 Butano Place, Fontana, CA, USA",
"price": 100,
"quantity": 3,
"trailer": "einzeln",
"vehicle": "Sattel",
"vehicleClass": "Mega",
"vehicleId": "123",
"vehicleReadyDate": "2019-09-26 22:57:36",
"weight": 12
},
{
"additionalPrices": 0,
"clientOrderNumber": "N / A",
"companyName": "Test Company",
"createdAt": "2019-09-06 22:46:25",
"createdBy": "test#test.com",
"deliveryDate": "2019-09-06 22:45:45",
"driver": "Michal Kucharski",
"dropOff": "QEW, Niagara Falls, ON, Canada",
"height": 67,
"isPreparingTrailer": false,
"isSmsSent": false,
"isTrailerReady": false,
"key": "d0ec3b82-2279-4d11-8e35-a9307713ae5a",
"notes": "This is coming along",
"pickUp": "Avenida Callao 1234, Buenos Aires, Argentina",
"price": 100,
"quantity": 1,
"trailer": "2er Pack",
"vehicle": "Sattel",
"vehicleClass": "Mega",
"vehicleId": "123",
"vehicleReadyDate": "2019-09-25 22:45:57",
"weight": 12
},
{
"additionalPrices": 0,
"clientOrderNumber": "N / A",
"companyName": "Test Company",
"createdAt": "2019-09-04 18:06:18",
"createdBy": "test#test.com",
"deliveryDate": "2019-09-05 18:05:51",
"driver": "Chibuzo ilogu",
"dropOff": "Asda Park Royal Superstore, Western Road, London, UK",
"height": 453,
"isPreparingTrailer": true,
"isSmsSent": true,
"isTrailerReady": true,
"key": "96ee9410-4d70-4bbc-8016-5d7c9e5ecec1",
"notes": "ewriupoi ",
"pickUp": "Avenida Juan Bautista Alberdi 1233, Buenos Aires, Argentina",
"price": 234,
"quantity": 1,
"trailer": "3er Pack",
"vehicle": "Anhänger",
"vehicleClass": "50",
"vehicleId": "543",
"vehicleReadyDate": "2019-09-21 18:06:03",
"weight": 453
},
{
"additionalPrices": 0,
"companyName": "Test Company",
"completeDate": "2019-09-04 18:02:27",
"createdAt": "2019-09-04 18:01:30",
"createdBy": "test#test.com",
"deliveryDate": "2019-09-04 18:01:09",
"driver": "Chibuzo ilogu",
"dropOff": "La Cabaña 123, Las Condes, Chile",
"height": 123,
"isPreparingTrailer": false,
"isSmsSent": true,
"isTrailerReady": true,
"key": "bcd50f43-0644-49a7-8bdc-009a4572341b",
"notes": "qewqe",
"pickUp": "La Cabaña 123, Las Condes, Chile",
"price": 123,
"trailer": "einzeln",
"vehicle": "Sattel",
"vehicleClass": "Standard",
"vehicleId": "123",
"vehicleReadyDate": "2019-10-02 18:01:19",
"weight": 123
}
];
export const handleFilterModifier = (value, compareValue, modifier) => {
if (modifier === 'lessThan') {
return value > compareValue;
}
if (modifier === 'equals') {
return value === compareValue;
}
if (modifier === 'greaterThan') {
return value < compareValue;
}
return null;
};
const resultList = list.filter(
item => Object.entries(queryModifiers).every(([filterKey, filterVal]) => {
const compareValue = item[filterKey];
const value = Object.values(queryKeys);
const result = handleFilterModifier(
parseFloat(value),
compareValue,
filterVal
);
return result;
})
);
This operation fails when either of the queryModifiers has "equals" in its value pair. Anything else returns the actual true result. I don't understand why it fails on "equals".
I feel like there should be a forEach somewhere but I don't know where to put it and maybe there's something wrong with the code. I need help figuring it out.
I think you should replace this
const queryKeys = keys: {
price: '1000',
weight: '1000'
}
with
const queryKeys = {
price: '1000',
weight: '1000'
}
and replace this
const value = Object.values(queryKeys);
with
const value = queryKeys[filterKey];
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
$("#loginsubmit").live("click", function() {
var request = new Request();
request.service = "http://88.80.223.163:8080/lumiin-service/lumiin/control/eventmap/1" ;
RequestHandler(request,displayData);
});
function displayData(data) {
//alert(data.contents.ownerId);
var cont = data.contents;
$("#mono").html(''+cont.id);
}
I am using the above code snippet to get the id, which works fine...How do I get the firstName?
Object:
{
"id": 1,
"eventId": {
"URL": "web",
"location": "web",
"url": "web",
"id": 54,
"type": {
"name": "Meeting",
"id": 1,
"isDeleted": "N",
"description": "Meeting"
},
"abteilung": {
"id": 165,
"code": "web123",
"isDeleted": null,
"domainId": 0,
"parentId": 164,
"description": null
},
"eventName": "Tanvi",
"place": "web",
"startDate": 1335830400000,
"endDate": 1337558400000,
"prospectId": null,
"person": "web",
"isDeleted": "N",
"countryId": {
"name": "CH",
"id": 1,
"isDeleted": "N",
"description": "Switzerland"
},
"followUp": null,
"budget": 0,
"description": "web"
},
"ownerId": {
"id": 67,
"firstName": "web123",
"localOrg": null,
"industryTypeId": null,
"memeber": false,
"profileType": "Manager",
"emailId": "web123",
"abteilung": {
"id": 165,
"code": "web123",
"isDeleted": null,
"domainId": 0,
"parentId": 164,
"description": null
},
"userCode": "web123",
"authenticatedAccess": -1,
"uuid": null,
"lastName": "web123",
"isDeleted": "N",
"passWord": "web123",
"fullName": "web123,web123",
"status": 0
},
"prospectId": {
"id": 303,
"firstName": "web123",
"emailId": "web123#gg.com",
"ownerId": {
"id": 67,
"firstName": "web123",
"localOrg": null,
"industryTypeId": null,
"memeber": false,
"profileType": "Manager",
"emailId": "web123",
"abteilung": {
"id": 165,
"code": "web123",
"isDeleted": null,
"domainId": 0,
"parentId": 164,
"description": null
},
"userCode": "web123",
"authenticatedAccess": -1,
"uuid": null,
"lastName": "web123",
"isDeleted": "N",
"passWord": "web123",
"fullName": "web123,web123",
"status": 0
},
"abteilung": {
"id": 165,
"code": "web123",
"isDeleted": null,
"domainId": 0,
"parentId": 164,
"description": null
},
"startDate": 1335744000000,
"endDate": 1337904000000,
"statusId": {
"name": "New",
"id": 1,
"isDeleted": "N",
"description": "New"
},
"company": "company",
"lastName": "web123",
"isDeleted": "N",
"yearofBirth": null,
"annualRevenue": 0,
"nationalityId": {
"name": "CH",
"id": 1,
"isDeleted": "N",
"description": "Switzerland"
},
"titleId": {
"name": "Mr",
"id": 1,
"isDeleted": "N",
"description": "Mr"
},
"maritalstatusId": null,
"residencyId": null,
"languageId": null,
"priorityId": {
"name": "Low",
"id": 1,
"isDeleted": "N",
"description": "Low"
},
"genderId": {
"name": "Male",
"id": 1,
"isDeleted": "N",
"description": "Male"
}
},
"isDeleted": "N"
}
The firstName is a property of the prospectId object you can read it in this way:
var firstName = data.prospectId.firstName;
var lastName = data.prospectId.lastName;
Fiddle