Javascript newbie here :) I want to achieve sth but can't figure it out.
Here is my data .
<pre>
[
[
{
"id": 2759178563,
"title": "Ergonomic Paper Computer",
"handle": "ergonomic-paper-computer",
"body_html": "Enable turn-key infrastructures",
"published_at": "2015-09-23T20:51:49-04:00",
"created_at": "2015-09-23T20:51:49-04:00",
"updated_at": "2015-09-23T20:51:49-04:00",
"vendor": "Schinner Inc",
"product_type": "Computer",
"tags": [
"Computer",
"Ergonomic",
"Paper"
],
"variants": [
{
"id": 8041863299,
"title": "White",
"option1": "White",
"option2": null,
"option3": null,
"price": "67.25",
"grams": 5145,
"compare_at_price": null,
"sku": "",
"requires_shipping": true,
"taxable": true,
"position": 1,
"product_id": 2759178563,
"created_at": "2015-09-23T20:51:49-04:00",
"updated_at": "2015-09-23T20:51:49-04:00",
"available": true,
"featured_image": null
},
{
"id": 8041863363,
"title": "Mint green",
"option1": "Mint green",
"option2": null,
"option3": null,
"price": "29.58",
"grams": 6860,
"compare_at_price": null,
"sku": "",
"requires_shipping": true,
"taxable": true,
"position": 2,
"product_id": 2759178563,
"created_at": "2015-09-23T20:51:49-04:00",
"updated_at": "2015-09-23T20:51:49-04:00",
"available": true,
"featured_image": null
},
{
"id": 8041863491,
"title": "Yellow",
"option1": "Yellow",
"option2": null,
"option3": null,
"price": "54.19",
"grams": 6045,
"compare_at_price": null,
"sku": "",
"requires_shipping": true,
"taxable": true,
"position": 3,
"product_id": 2759178563,
"created_at": "2015-09-23T20:51:49-04:00",
"updated_at": "2015-09-23T20:51:49-04:00",
"available": true,
"featured_image": null
},
{
"id": 8041863555,
"title": "Blue",
"option1": "Blue",
"option2": null,
"option3": null,
"price": "22.88",
"grams": 9526,
"compare_at_price": null,
"sku": "",
"requires_shipping": true,
"taxable": true,
"position": 4,
"product_id": 2759178563,
"created_at": "2015-09-23T20:51:49-04:00",
"updated_at": "2015-09-23T20:51:49-04:00",
"available": true,
"featured_image": null
}
],
"images": [
{
"id": 5642046019,
"created_at": "2015-09-23T20:51:49-04:00",
"position": 1,
"updated_at": "2015-09-23T20:51:49-04:00",
"product_id": 2759178563,
"variant_ids": [],
"src": "https://cdn.shopify.com/s/files/1/1000/7970/products/Ergonomic_20Paper_20Computer.png?v=1443055909"
}
],
"options": [
{
"name": "Title",
"position": 1,
"values": [
"White",
"Mint green",
"Yellow",
"Blue"
]
}
]
},
{
"id": 2759192387,
"title": "Heavy Duty Concrete Keyboard",
"handle": "heavy-duty-concrete-keyboard",
"body_html": "Strategize synergistic e-markets",
"published_at": "2015-09-23T20:52:07-04:00",
"created_at": "2015-09-23T20:52:07-04:00",
"updated_at": "2015-09-23T20:52:07-04:00",
"vendor": "Stiedemann and Sons",
"product_type": "Keyboard",
"tags": [
"Concrete",
"Duty",
"Heavy",
"Keyboard"
],
"variants": [
{
"id": 8041883779,
"title": "Magenta",
"option1": "Magenta",
"option2": null,
"option3": null,
"price": "14.20",
"grams": 3030,
"compare_at_price": null,
"sku": "",
"requires_shipping": true,
"taxable": true,
"position": 1,
"product_id": 2759192387,
"created_at": "2015-09-23T20:52:07-04:00",
"updated_at": "2015-09-23T20:52:07-04:00",
"available": true,
"featured_image": null
}
],
"images": [
{
"id": 5642056899,
"created_at": "2015-09-23T20:52:07-04:00",
"position": 1,
"updated_at": "2015-09-23T20:52:07-04:00",
"product_id": 2759192387,
"variant_ids": [],
"src": "https://cdn.shopify.com/s/files/1/1000/7970/products/Heavy_20Duty_20Concrete_20Keyboard.png?v=1443055927"
}
],
"options": [
{
"name": "Title",
"position": 1,
"values": [
"Magenta"
]
}
]
}
],
[
{
"id": 2759168323,
"title": "Awesome Cotton Computer",
"handle": "awesome-cotton-computer-1",
"body_html": "Brand synergistic applications",
"published_at": "2015-09-23T20:51:24-04:00",
"created_at": "2015-09-23T20:51:24-04:00",
"updated_at": "2015-09-23T20:51:24-04:00",
"vendor": "Hills Group",
"product_type": "Computer",
"tags": [
"Awesome",
"Computer",
"Cotton"
],
"variants": [
{
"id": 8041841795,
"title": "Black",
"option1": "Black",
"option2": null,
"option3": null,
"price": "2.05",
"grams": 1906,
"compare_at_price": null,
"sku": "",
"requires_shipping": true,
"taxable": true,
"position": 1,
"product_id": 2759168323,
"created_at": "2015-09-23T20:51:24-04:00",
"updated_at": "2015-09-23T20:51:24-04:00",
"available": true,
"featured_image": null
},
{
"id": 8041841859,
"title": "Orchid",
"option1": "Orchid",
"option2": null,
"option3": null,
"price": "10.78",
"grams": 4970,
"compare_at_price": null,
"sku": "",
"requires_shipping": true,
"taxable": true,
"position": 2,
"product_id": 2759168323,
"created_at": "2015-09-23T20:51:24-04:00",
"updated_at": "2015-09-23T20:51:24-04:00",
"available": true,
"featured_image": null
},
{
"id": 8041841923,
"title": "Tan",
"option1": "Tan",
"option2": null,
"option3": null,
"price": "50.54",
"grams": 6738,
"compare_at_price": null,
"sku": "",
"requires_shipping": true,
"taxable": true,
"position": 3,
"product_id": 2759168323,
"created_at": "2015-09-23T20:51:24-04:00",
"updated_at": "2015-09-23T20:51:24-04:00",
"available": true,
"featured_image": null
},
{
"id": 8041841987,
"title": "Teal",
"option1": "Teal",
"option2": null,
"option3": null,
"price": "91.51",
"grams": 8718,
"compare_at_price": null,
"sku": "",
"requires_shipping": true,
"taxable": true,
"position": 4,
"product_id": 2759168323,
"created_at": "2015-09-23T20:51:24-04:00",
"updated_at": "2015-09-23T20:51:24-04:00",
"available": true,
"featured_image": null
},
{
"id": 8041842051,
"title": "Gold",
"option1": "Gold",
"option2": null,
"option3": null,
"price": "8.24",
"grams": 194,
"compare_at_price": null,
"sku": "",
"requires_shipping": true,
"taxable": true,
"position": 5,
"product_id": 2759168323,
"created_at": "2015-09-23T20:51:24-04:00",
"updated_at": "2015-09-23T20:51:24-04:00",
"available": true,
"featured_image": null
}
],
"images": [
{
"id": 5642032131,
"created_at": "2015-09-23T20:51:24-04:00",
"position": 1,
"updated_at": "2015-09-23T20:51:24-04:00",
"product_id": 2759168323,
"variant_ids": [],
"src": "https://cdn.shopify.com/s/files/1/1000/7970/products/Awesome_20Cotton_20Computer_f74fc2a4-efa5-42ca-a3b2-36c378f1c003.png?v=1443055884"
}
],
"options": [
{
"name": "Title",
"position": 1,
"values": [
"Black",
"Orchid",
"Tan",
"Teal",
"Gold"
]
}
]
},
{
"id": 2759192387,
"title": "Heavy Duty Concrete Keyboard",
"handle": "heavy-duty-concrete-keyboard",
"body_html": "Strategize synergistic e-markets",
"published_at": "2015-09-23T20:52:07-04:00",
"created_at": "2015-09-23T20:52:07-04:00",
"updated_at": "2015-09-23T20:52:07-04:00",
"vendor": "Stiedemann and Sons",
"product_type": "Keyboard",
"tags": [
"Concrete",
"Duty",
"Heavy",
"Keyboard"
],
"variants": [
{
"id": 8041883779,
"title": "Magenta",
"option1": "Magenta",
"option2": null,
"option3": null,
"price": "14.20",
"grams": 3030,
"compare_at_price": null,
"sku": "",
"requires_shipping": true,
"taxable": true,
"position": 1,
"product_id": 2759192387,
"created_at": "2015-09-23T20:52:07-04:00",
"updated_at": "2015-09-23T20:52:07-04:00",
"available": true,
"featured_image": null
}
],
"images": [
{
"id": 5642056899,
"created_at": "2015-09-23T20:52:07-04:00",
"position": 1,
"updated_at": "2015-09-23T20:52:07-04:00",
"product_id": 2759192387,
"variant_ids": [],
"src": "https://cdn.shopify.com/s/files/1/1000/7970/products/Heavy_20Duty_20Concrete_20Keyboard.png?v=1443055927"
}
],
"options": [
{
"name": "Title",
"position": 1,
"values": [
"Magenta"
]
}
]
}
],
[
{
"id": 2759167747,
"title": "Awesome Bronze Computer",
"handle": "awesome-bronze-computer",
"body_html": "Orchestrate holistic web services",
"published_at": "2015-09-23T20:51:20-04:00",
"created_at": "2015-09-23T20:51:20-04:00",
"updated_at": "2015-09-23T20:51:20-04:00",
"vendor": "Zboncak-Kemmer",
"product_type": "Computer",
"tags": [
"Awesome",
"Bronze",
"Computer"
],
"variants": [
{
"id": 8041840195,
"title": "Magenta",
"option1": "Magenta",
"option2": null,
"option3": null,
"price": "56.69",
"grams": 6035,
"compare_at_price": null,
"sku": "",
"requires_shipping": true,
"taxable": true,
"position": 1,
"product_id": 2759167747,
"created_at": "2015-09-23T20:51:20-04:00",
"updated_at": "2015-09-23T20:51:20-04:00",
"available": true,
"featured_image": null
},
{
"id": 8041840259,
"title": "Mint green",
"option1": "Mint green",
"option2": null,
"option3": null,
"price": "63.89",
"grams": 9961,
"compare_at_price": null,
"sku": "",
"requires_shipping": true,
"taxable": true,
"position": 2,
"product_id": 2759167747,
"created_at": "2015-09-23T20:51:20-04:00",
"updated_at": "2015-09-23T20:51:20-04:00",
"available": true,
"featured_image": null
},
{
"id": 8041840323,
"title": "Maroon",
"option1": "Maroon",
"option2": null,
"option3": null,
"price": "18.76",
"grams": 922,
"compare_at_price": null,
"sku": "",
"requires_shipping": true,
"taxable": true,
"position": 3,
"product_id": 2759167747,
"created_at": "2015-09-23T20:51:20-04:00",
"updated_at": "2015-09-23T20:51:20-04:00",
"available": true,
"featured_image": null
},
{
"id": 8041840387,
"title": "Salmon",
"option1": "Salmon",
"option2": null,
"option3": null,
"price": "97.60",
"grams": 1355,
"compare_at_price": null,
"sku": "",
"requires_shipping": true,
"taxable": true,
"position": 4,
"product_id": 2759167747,
"created_at": "2015-09-23T20:51:20-04:00",
"updated_at": "2015-09-23T20:51:20-04:00",
"available": true,
"featured_image": null
}
],
"images": [
{
"id": 5642031235,
"created_at": "2015-09-23T20:51:20-04:00",
"position": 1,
"updated_at": "2015-09-23T20:51:20-04:00",
"product_id": 2759167747,
"variant_ids": [],
"src": "https://cdn.shopify.com/s/files/1/1000/7970/products/Awesome_20Bronze_20Computer.png?v=1443055880"
}
],
"options": [
{
"name": "Title",
"position": 1,
"values": [
"Magenta",
"Mint green",
"Maroon",
"Salmon"
]
}
]
},
{
"id": 2759192387,
"title": "Heavy Duty Concrete Keyboard",
"handle": "heavy-duty-concrete-keyboard",
"body_html": "Strategize synergistic e-markets",
"published_at": "2015-09-23T20:52:07-04:00",
"created_at": "2015-09-23T20:52:07-04:00",
"updated_at": "2015-09-23T20:52:07-04:00",
"vendor": "Stiedemann and Sons",
"product_type": "Keyboard",
"tags": [
"Concrete",
"Duty",
"Heavy",
"Keyboard"
],
"variants": [
{
"id": 8041883779,
"title": "Magenta",
"option1": "Magenta",
"option2": null,
"option3": null,
"price": "14.20",
"grams": 3030,
"compare_at_price": null,
"sku": "",
"requires_shipping": true,
"taxable": true,
"position": 1,
"product_id": 2759192387,
"created_at": "2015-09-23T20:52:07-04:00",
"updated_at": "2015-09-23T20:52:07-04:00",
"available": true,
"featured_image": null
}
],
"images": [
{
"id": 5642056899,
"created_at": "2015-09-23T20:52:07-04:00",
"position": 1,
"updated_at": "2015-09-23T20:52:07-04:00",
"product_id": 2759192387,
"variant_ids": [],
"src": "https://cdn.shopify.com/s/files/1/1000/7970/products/Heavy_20Duty_20Concrete_20Keyboard.png?v=1443055927"
}
],
"options": [
{
"name": "Title",
"position": 1,
"values": [
"Magenta"
]
}
]
}
]
]
</pre>
And here is my code:
import { Injectable } from '#angular/core';
import { Http, Response } from '#angular/http';
#Injectable()
export class ShopifyService {
/**
* Initialization
*/
constructor (
private http: Http
) {}
private shopifyUrl = 'app/heroes.json';
errorMessage: string;
/**
* Private Methods
*/
fetchProducts(): Promise<Response> {
return this.http.get(this.shopifyUrl)
.toPromise()
.then(this.extractData)
.catch(this.handleError);
}
private extractData(res: Response) {
if (res.status < 200 || res.status >= 300) {
throw new Error('Bad response status: ' + res.status);
}
let body = res.json();
if (body.data) {
return body.data;
}
else if (body.products) {
return body.products;
}
else {
return { };
}
}
private handleError (error: any) {
let errMsg = error.message || 'Server error';
console.error(errMsg);
return Promise.reject(errMsg);
}
private filterProducts(list, archetype) {
var result = [];
for (var i = 0; i < list.length; i++) {
if (list[i].product_type === archetype) {
result.push(list[i]);
}
}
return result;
}
/**
* Public Methods
*/
public findProducts(archetypes) {
return this.fetchProducts().then(
products => {
var result = [],
fetchedResponse = [];
for (var i = 0; i < archetypes.length; i++) {
fetchedResponse = this.filterProducts(
products, archetypes[i]
);
result = result.concat(fetchedResponse);
}
return result;
},
error => this.errorMessage = <any>error
)
}
public findSingleProductVariant() {
var result = [],
j = 0;
return this.findVariants().then(
product => {
for (var i = 0; i < product.length; i++) {
// console.log(product[i]);
}
}
);
// return result;
}
findVariants() {
return this.findProducts(['Computer', 'Keyboard']).then(
products => {
var result = [];
for (var i = 0; i < products.length; i++) {
if (products[i].product_type === 'Computer') {
for (var j = 0; j < products.length; j++) {
if (products[j].product_type === 'Keyboard') {
result.push([products[i], products[j]]);
}
}
}
}
console.log(result);
return result;
}
)
}
}
Until here I got to half of what. An array of two dimensional arrays (one of them is from keyboard type and another one from computer type).
It is sth like this ==>
[
[
[Computer_A],
[Keyboard_A]
],
[
[Computer_B],
[Keyboard_A]
],
[
[Computer_C],
[Keyboard_A]
],
]
Now each one of these objects have its own array of variants. I have to modify my findSingleProductVariant in order to go over each one of these arrays, pick a keyboard variant (which its array has a price field), also pick a computer variant and calculate the price. If the price is less than 1000, it can continue picking another keyboard and computer. But the thing is that it can't have two identical arrays. for instance we can't have sth like this.
[
['Computer_A'], ['Keyboard_A'],
['Computer_A'], ['Keyboard_A'],
]
The key thing here is that you can't directly compare objects and expect relevant results, e.g.
['Red Lamp', 'Blue Table'] === ['Red Lamp', 'Blue Table']
will return false (Array.indexOf() also won't work as expected since it uses strict comparison).
So for an array of arrays, you will have to use two nested loops to get down to comparing primitive values.
Related
I'm getting a response from an API, I'm interested only in one of the link that returns. I have been using for a while
["data"]["tasks"]["0"]["result"]["files"]["0"]["url"]
to get the URL but it seems the API has changed and now the "result" it changes the position 0 in every call...
{"data": {
"id": "dcd99049-63b2",
"tag": "jobbuilder",
"status": "finished",
"created_at": "2022-11-22T21:57:28+00:00",
"started_at": "2022-11-22T21:57:28+00:00",
"ended_at": "2022-11-22T21:57:33+00:00",
"tasks": [
{
"id": "48c34a03-54f2",
"name": "task-2",
"job_id": "dcd99049-63b2",
"status": "finished",
"credits": 0,
"code": null,
"message": null,
"percent": 100,
"operation": "convert",
"engine": "qpdf",
"engine_version": "10.1.0",
"result": {
"files": [
{
"filename": "something.pdf",
"size": 633487
}
]
},
"created_at": "2022-11-22T21:57:30+00:00",
"started_at": "2022-11-22T21:57:32+00:00",
"ended_at": "2022-11-22T21:57:32+00:00",
"retry_of_task_id": null,
"copy_of_task_id": "dd64385a-a2e9",
"user_id": 6070,
"priority": 10,
"host_name": "meggie",
"storage": null,
"depends_on_task_ids": [
"dd64385a-a2e9",
"acfd2a2c-3c45"
],
"links": {
"self": "https://api.cloudconvert.com/v2/tasks/48c34a03-54f2"
}
},
{
"id": "6333e3b8-2f6f",
"name": "export-1",
"job_id": "dcd99049-63b2",
"status": "finished",
"credits": 0,
"code": null,
"message": null,
"percent": 100,
"operation": "export/url",
"result": {
"files": [
{
"filename": "something.pdf",
"size": 633487,
"url": "https://storage.cloudconvert.com/tasks/somethinglink.pdf"
}
]
},
"created_at": "2022-11-22T21:57:28+00:00",
"started_at": "2022-11-22T21:57:32+00:00",
"ended_at": "2022-11-22T21:57:32+00:00",
"retry_of_task_id": null,
"copy_of_task_id": null,
"user_id": 607,
"priority": 10,
"host_name": "meggie",
"storage": "ceph-fra",
"depends_on_task_ids": [
"48c34a03-54f2"
],
"links": {
"self": "https://api.cloudconvert.com/v2/tasks/6333e3b8-2f6f"
}
},
{
"id": "dd64385a-a2e9",
"name": "task-2-create-watermark",
"job_id": "dcd99049-63b2",
"status": "finished",
"credits": 1,
"code": null,
"message": null,
"percent": 100,
"operation": "watermark",
"engine": "imagemagick",
"engine_version": "7.1.0",
"result": {
"files": [
{
"filename": "watermark.pdf",
"size": 609229
}
]
},
"created_at": "2022-11-22T21:57:28+00:00",
"started_at": "2022-11-22T21:57:30+00:00",
"ended_at": "2022-11-22T21:57:32+00:00",
"retry_of_task_id": null,
"copy_of_task_id": null,
"user_id": 6070,
"priority": 10,
"host_name": "meggie",
"storage": "ceph-fra",
"depends_on_task_ids": [
"acfd2a2c-3c45-4e47"
],
"links": {
"self": "https://api.cloudconvert.com/v2/tasks/dd64385a-a2e9"
}
},
{
"id": "acfd2a2c-3c45-4e47",
"name": "task-1",
"job_id": "dcd99049-63b2",
"status": "finished",
"credits": 1,
"code": null,
"message": null,
"percent": 100,
"operation": "capture-website",
"engine": "wkhtml",
"engine_version": "0.12.5",
"result": {
"files": [
{
"filename": "something.pdf",
"size": 27330
}
]
},
"created_at": "2022-11-22T21:57:28+00:00",
"started_at": "2022-11-22T21:57:28+00:00",
"ended_at": "2022-11-22T21:57:30+00:00",
"retry_of_task_id": null,
"copy_of_task_id": null,
"user_id": 6070,
"priority": 10,
"host_name": "meggie",
"storage": null,
"depends_on_task_ids": [],
"links": {
"self": "https://api.cloudconvert.com/v2/tasks/acfd2a2c-3c45"
}
}
],
"links": {
"self": "https://api.cloudconvert.com/v2/jobs/dcd99049-63b2-443e"
}
}};
I would like to always extract only the link:
https://storage.cloudconvert.com/tasks/somethinglink.pdf
which position can change as mentioned before... The link always start with https://storage.cloudconvert.com
I have been using this for a while to access the first object in the array
["data"]["tasks"]["0"]["result"]["files"]["0"]["url"]
but it doesn't seem to work if the link it's in a different position...
This code extracts all values as strings, then finds the one with the necessary prefix:
const data = {"data":{"id":"dcd99049-63b2","tag":"jobbuilder","status":"finished","created_at":"2022-11-22T21:57:28+00:00","started_at":"2022-11-22T21:57:28+00:00","ended_at":"2022-11-22T21:57:33+00:00","tasks":[{"id":"48c34a03-54f2","name":"task-2","job_id":"dcd99049-63b2","status":"finished","credits":0,"code":null,"message":null,"percent":100,"operation":"convert","engine":"qpdf","engine_version":"10.1.0","result":{"files":[{"filename":"something.pdf","size":633487}]},"created_at":"2022-11-22T21:57:30+00:00","started_at":"2022-11-22T21:57:32+00:00","ended_at":"2022-11-22T21:57:32+00:00","retry_of_task_id":null,"copy_of_task_id":"dd64385a-a2e9","user_id":6070,"priority":10,"host_name":"meggie","storage":null,"depends_on_task_ids":["dd64385a-a2e9","acfd2a2c-3c45"],"links":{"self":"https://api.cloudconvert.com/v2/tasks/48c34a03-54f2"}},{"id":"6333e3b8-2f6f","name":"export-1","job_id":"dcd99049-63b2","status":"finished","credits":0,"code":null,"message":null,"percent":100,"operation":"export/url","result":{"files":[{"filename":"something.pdf","size":633487,"url":"https://storage.cloudconvert.com/tasks/somethinglink.pdf"}]},"created_at":"2022-11-22T21:57:28+00:00","started_at":"2022-11-22T21:57:32+00:00","ended_at":"2022-11-22T21:57:32+00:00","retry_of_task_id":null,"copy_of_task_id":null,"user_id":607,"priority":10,"host_name":"meggie","storage":"ceph-fra","depends_on_task_ids":["48c34a03-54f2"],"links":{"self":"https://api.cloudconvert.com/v2/tasks/6333e3b8-2f6f"}},{"id":"dd64385a-a2e9","name":"task-2-create-watermark","job_id":"dcd99049-63b2","status":"finished","credits":1,"code":null,"message":null,"percent":100,"operation":"watermark","engine":"imagemagick","engine_version":"7.1.0","result":{"files":[{"filename":"watermark.pdf","size":609229}]},"created_at":"2022-11-22T21:57:28+00:00","started_at":"2022-11-22T21:57:30+00:00","ended_at":"2022-11-22T21:57:32+00:00","retry_of_task_id":null,"copy_of_task_id":null,"user_id":6070,"priority":10,"host_name":"meggie","storage":"ceph-fra","depends_on_task_ids":["acfd2a2c-3c45-4e47"],"links":{"self":"https://api.cloudconvert.com/v2/tasks/dd64385a-a2e9"}},{"id":"acfd2a2c-3c45-4e47","name":"task-1","job_id":"dcd99049-63b2","status":"finished","credits":1,"code":null,"message":null,"percent":100,"operation":"capture-website","engine":"wkhtml","engine_version":"0.12.5","result":{"files":[{"filename":"something.pdf","size":27330}]},"created_at":"2022-11-22T21:57:28+00:00","started_at":"2022-11-22T21:57:28+00:00","ended_at":"2022-11-22T21:57:30+00:00","retry_of_task_id":null,"copy_of_task_id":null,"user_id":6070,"priority":10,"host_name":"meggie","storage":null,"depends_on_task_ids":[],"links":{"self":"https://api.cloudconvert.com/v2/tasks/acfd2a2c-3c45"}}],"links":{"self":"https://api.cloudconvert.com/v2/jobs/dcd99049-63b2-443e"}}}
const f=i=>i instanceof Object?Object.values(i).flatMap(v=>f(v)):[i+'']
console.log(f(data).find(i=>
i.startsWith('https://storage.cloudconvert.com/tasks/')))
I have a nested object which can be of any depth
[
{
"id": "3",
"user_id": "1479",
"folder_id": "2",
"is_folder": true,
"folder_name": "folder 3",
"unique_filename": null,
"original_filename": null,
"file_extension": null,
"content_type": null,
"file_size": null,
"description": "People",
"visible": false,
"status": 0,
"deleted_at": null,
"last_access": "2022-11-21T09:34:48.546+02:00",
"created_at": "2022-11-21T09:34:48.546+02:00",
"updated_at": "2022-11-21T09:34:48.546+02:00",
"children": [
{
"id": "36",
"user_id": "1479",
"folder_id": "3",
"is_folder": true,
"folder_name": "folder 4",
"unique_filename": null,
"original_filename": null,
"file_extension": null,
"content_type": null,
"file_size": null,
"description": "People",
"visible": false,
"status": 0,
"deleted_at": null,
"last_access": "2022-11-21T21:33:48.767+02:00",
"created_at": "2022-11-21T21:33:48.767+02:00",
"updated_at": "2022-11-21T21:33:48.767+02:00",
"children": [
{
"id": "37",
"user_id": "1479",
"folder_id": "36",
"is_folder": true,
"folder_name": "folder 37",
"unique_filename": null,
"original_filename": null,
"file_extension": null,
"content_type": null,
"file_size": null,
"description": "People",
"visible": false,
"status": 0,
"deleted_at": null,
"last_access": "2022-11-21T21:38:30.690+02:00",
"created_at": "2022-11-21T21:38:30.690+02:00",
"updated_at": "2022-11-21T21:38:30.690+02:00",
"children": []
}
]
},
{
"id": "42",
"user_id": "1479",
"folder_id": "3",
"is_folder": true,
"folder_name": "folder 41",
"unique_filename": null,
"original_filename": null,
"file_extension": null,
"content_type": null,
"file_size": null,
"description": "People",
"visible": false,
"status": 0,
"deleted_at": null,
"last_access": "2022-11-21T23:38:31.935+02:00",
"created_at": "2022-11-21T23:38:31.935+02:00",
"updated_at": "2022-11-21T23:38:31.935+02:00",
"children": []
}
]
},
{
"id": "5",
"user_id": "1479",
"folder_id": null,
"is_folder": false,
"folder_name": null,
"unique_filename": "drives/users/user_drive_1479/YTHGg4dnzn8O5a4DGHbntsrKhY2n4ycc3hZG5j7YxdIb5yEka9iToJDi9WxQPe4taSjLP53b1s01mctIy69o7m6L92.c",
"original_filename": "sample3.c",
"file_extension": "c",
"content_type": "text/x-c",
"file_size": "126",
"description": null,
"visible": false,
"status": 0,
"deleted_at": null,
"last_access": "2022-11-21T09:37:48.766+02:00",
"created_at": "2022-11-21T09:37:48.767+02:00",
"updated_at": "2022-11-21T09:37:48.767+02:00",
"children": []
},
{
"id": "7",
"user_id": "1479",
"folder_id": null,
"is_folder": false,
"folder_name": null,
"unique_filename": "drives/users/user_drive_1479/FSix4WZx0s9ey89x3foLxmaC1wCHTSw1HQi8fDxQ32bYQyKmyPJBcgeI33KOrdPfAcOChvkBnIBizj5IQbggeprCpz.cpp",
"original_filename": "sample1.cpp",
"file_extension": "cpp",
"content_type": "text/x-c",
"file_size": "94",
"description": null,
"visible": false,
"status": 0,
"deleted_at": null,
"last_access": "2022-11-21T09:37:52.324+02:00",
"created_at": "2022-11-21T09:37:52.324+02:00",
"updated_at": "2022-11-21T09:37:52.324+02:00",
"children": []
},
{
"id": "37",
"user_id": "1479",
"folder_id": "36",
"is_folder": true,
"folder_name": "folder 37",
"unique_filename": null,
"original_filename": null,
"file_extension": null,
"content_type": null,
"file_size": null,
"description": "People",
"visible": false,
"status": 0,
"deleted_at": null,
"last_access": "2022-11-21T21:38:30.690+02:00",
"created_at": "2022-11-21T21:38:30.690+02:00",
"updated_at": "2022-11-21T21:38:30.690+02:00",
"children": []
}
]
I need to get the value of all the id keys
from the array of objects above the expected outcome would be
[3,36,37,42,5,7,37]
the code I have tried is shown below
if (data.children) {
console.log(data.id)
data.children.forEach(item => {
this.getObject(item)
})
} else {
console.log(data.id)
}
the data variable being the array of objects above
I get undefined as a result.
your assistance would be much appreciated
Using a recursive technique can cause a stack overflow if the node treelist is too numerous. An iterative approach is safer:
TS Playground
type NodeWithChildren<T> = T & { children?: T[] };
function collectNodeValues <
K extends string,
T extends NodeWithChildren<Record<K, unknown>>,
>(nodeList: readonly T[], targetProperty: K): T[K][] {
const stack: T[] = [...nodeList];
const values: T[K][] = [];
while (stack.length > 0) {
const node = stack.shift()!;
values.push(node[targetProperty]);
if (!Array.isArray(node.children)) continue;
stack.unshift(...node.children);
}
return values;
}
const input = [{"id":"3","user_id":"1479","folder_id":"2","is_folder":true,"folder_name":"folder 3","unique_filename":null,"original_filename":null,"file_extension":null,"content_type":null,"file_size":null,"description":"People","visible":false,"status":0,"deleted_at":null,"last_access":"2022-11-21T09:34:48.546+02:00","created_at":"2022-11-21T09:34:48.546+02:00","updated_at":"2022-11-21T09:34:48.546+02:00","children":[{"id":"36","user_id":"1479","folder_id":"3","is_folder":true,"folder_name":"folder 4","unique_filename":null,"original_filename":null,"file_extension":null,"content_type":null,"file_size":null,"description":"People","visible":false,"status":0,"deleted_at":null,"last_access":"2022-11-21T21:33:48.767+02:00","created_at":"2022-11-21T21:33:48.767+02:00","updated_at":"2022-11-21T21:33:48.767+02:00","children":[{"id":"37","user_id":"1479","folder_id":"36","is_folder":true,"folder_name":"folder 37","unique_filename":null,"original_filename":null,"file_extension":null,"content_type":null,"file_size":null,"description":"People","visible":false,"status":0,"deleted_at":null,"last_access":"2022-11-21T21:38:30.690+02:00","created_at":"2022-11-21T21:38:30.690+02:00","updated_at":"2022-11-21T21:38:30.690+02:00","children":[]}]},{"id":"42","user_id":"1479","folder_id":"3","is_folder":true,"folder_name":"folder 41","unique_filename":null,"original_filename":null,"file_extension":null,"content_type":null,"file_size":null,"description":"People","visible":false,"status":0,"deleted_at":null,"last_access":"2022-11-21T23:38:31.935+02:00","created_at":"2022-11-21T23:38:31.935+02:00","updated_at":"2022-11-21T23:38:31.935+02:00","children":[]}]},{"id":"5","user_id":"1479","folder_id":null,"is_folder":false,"folder_name":null,"unique_filename":"drives/users/user_drive_1479/YTHGg4dnzn8O5a4DGHbntsrKhY2n4ycc3hZG5j7YxdIb5yEka9iToJDi9WxQPe4taSjLP53b1s01mctIy69o7m6L92.c","original_filename":"sample3.c","file_extension":"c","content_type":"text/x-c","file_size":"126","description":null,"visible":false,"status":0,"deleted_at":null,"last_access":"2022-11-21T09:37:48.766+02:00","created_at":"2022-11-21T09:37:48.767+02:00","updated_at":"2022-11-21T09:37:48.767+02:00","children":[]},{"id":"7","user_id":"1479","folder_id":null,"is_folder":false,"folder_name":null,"unique_filename":"drives/users/user_drive_1479/FSix4WZx0s9ey89x3foLxmaC1wCHTSw1HQi8fDxQ32bYQyKmyPJBcgeI33KOrdPfAcOChvkBnIBizj5IQbggeprCpz.cpp","original_filename":"sample1.cpp","file_extension":"cpp","content_type":"text/x-c","file_size":"94","description":null,"visible":false,"status":0,"deleted_at":null,"last_access":"2022-11-21T09:37:52.324+02:00","created_at":"2022-11-21T09:37:52.324+02:00","updated_at":"2022-11-21T09:37:52.324+02:00","children":[]},{"id":"37","user_id":"1479","folder_id":"36","is_folder":true,"folder_name":"folder 37","unique_filename":null,"original_filename":null,"file_extension":null,"content_type":null,"file_size":null,"description":"People","visible":false,"status":0,"deleted_at":null,"last_access":"2022-11-21T21:38:30.690+02:00","created_at":"2022-11-21T21:38:30.690+02:00","updated_at":"2022-11-21T21:38:30.690+02:00","children":[]}];
const ids = collectNodeValues(input, 'id');
console.log(ids); // ["3", "36", "37", "42", "5", "7", "37"]
Compiled JS from the TS Playground:
"use strict";
function collectNodeValues(nodeList, targetProperty) {
const stack = [...nodeList];
const values = [];
while (stack.length > 0) {
const node = stack.shift();
values.push(node[targetProperty]);
if (!Array.isArray(node.children))
continue;
stack.unshift(...node.children);
}
return values;
}
const input = [{ "id": "3", "user_id": "1479", "folder_id": "2", "is_folder": true, "folder_name": "folder 3", "unique_filename": null, "original_filename": null, "file_extension": null, "content_type": null, "file_size": null, "description": "People", "visible": false, "status": 0, "deleted_at": null, "last_access": "2022-11-21T09:34:48.546+02:00", "created_at": "2022-11-21T09:34:48.546+02:00", "updated_at": "2022-11-21T09:34:48.546+02:00", "children": [{ "id": "36", "user_id": "1479", "folder_id": "3", "is_folder": true, "folder_name": "folder 4", "unique_filename": null, "original_filename": null, "file_extension": null, "content_type": null, "file_size": null, "description": "People", "visible": false, "status": 0, "deleted_at": null, "last_access": "2022-11-21T21:33:48.767+02:00", "created_at": "2022-11-21T21:33:48.767+02:00", "updated_at": "2022-11-21T21:33:48.767+02:00", "children": [{ "id": "37", "user_id": "1479", "folder_id": "36", "is_folder": true, "folder_name": "folder 37", "unique_filename": null, "original_filename": null, "file_extension": null, "content_type": null, "file_size": null, "description": "People", "visible": false, "status": 0, "deleted_at": null, "last_access": "2022-11-21T21:38:30.690+02:00", "created_at": "2022-11-21T21:38:30.690+02:00", "updated_at": "2022-11-21T21:38:30.690+02:00", "children": [] }] }, { "id": "42", "user_id": "1479", "folder_id": "3", "is_folder": true, "folder_name": "folder 41", "unique_filename": null, "original_filename": null, "file_extension": null, "content_type": null, "file_size": null, "description": "People", "visible": false, "status": 0, "deleted_at": null, "last_access": "2022-11-21T23:38:31.935+02:00", "created_at": "2022-11-21T23:38:31.935+02:00", "updated_at": "2022-11-21T23:38:31.935+02:00", "children": [] }] }, { "id": "5", "user_id": "1479", "folder_id": null, "is_folder": false, "folder_name": null, "unique_filename": "drives/users/user_drive_1479/YTHGg4dnzn8O5a4DGHbntsrKhY2n4ycc3hZG5j7YxdIb5yEka9iToJDi9WxQPe4taSjLP53b1s01mctIy69o7m6L92.c", "original_filename": "sample3.c", "file_extension": "c", "content_type": "text/x-c", "file_size": "126", "description": null, "visible": false, "status": 0, "deleted_at": null, "last_access": "2022-11-21T09:37:48.766+02:00", "created_at": "2022-11-21T09:37:48.767+02:00", "updated_at": "2022-11-21T09:37:48.767+02:00", "children": [] }, { "id": "7", "user_id": "1479", "folder_id": null, "is_folder": false, "folder_name": null, "unique_filename": "drives/users/user_drive_1479/FSix4WZx0s9ey89x3foLxmaC1wCHTSw1HQi8fDxQ32bYQyKmyPJBcgeI33KOrdPfAcOChvkBnIBizj5IQbggeprCpz.cpp", "original_filename": "sample1.cpp", "file_extension": "cpp", "content_type": "text/x-c", "file_size": "94", "description": null, "visible": false, "status": 0, "deleted_at": null, "last_access": "2022-11-21T09:37:52.324+02:00", "created_at": "2022-11-21T09:37:52.324+02:00", "updated_at": "2022-11-21T09:37:52.324+02:00", "children": [] }, { "id": "37", "user_id": "1479", "folder_id": "36", "is_folder": true, "folder_name": "folder 37", "unique_filename": null, "original_filename": null, "file_extension": null, "content_type": null, "file_size": null, "description": "People", "visible": false, "status": 0, "deleted_at": null, "last_access": "2022-11-21T21:38:30.690+02:00", "created_at": "2022-11-21T21:38:30.690+02:00", "updated_at": "2022-11-21T21:38:30.690+02:00", "children": [] }];
const ids = collectNodeValues(input, 'id');
console.log(ids); // ["3", "36", "37", "42", "5", "7", "37"]
Considering that the schema of the object is like what you have written, first you can define a variable to store the id list.
let idList = [];
Then write a recursive function to get Ids like this:
function getId(obj){
obj?.map(item =>{
if(item?.id){
idList.push(item.id);
}
if(item?.children && item.children.length){
getId(item.children);
}
})
}
Next call the function and pass the object that you want, to fill idList.
Im trying to filter posts by categories from this array
Array [
Object {
"category": 1,
"content": "For 50 years, AFP has been the standard-bearer for professionalism in fundraising. Learn more about AFP, its activities and people, and how you can be involved.
[Link1](https://cso-web.herokuapp.com)",
"content_preview": null,
"coverImageSubtitle": null,
"coverImageTitle": null,
"created_at": "2021-05-13T18:34:17.260Z",
"custom_link": null,
"gallery": Array [],
"id": 1,
"isDeleted": null,
"locale": "sq",
"localizations": Array [],
"pages": Array [
Object {
"commitments": null,
"content_sub_menu": null,
"created_at": "2021-05-19T21:19:15.101Z",
"id": 18,
"isActive": true,
"locale": "sq",
"name": "Member Listing",
"showOnFooterMenu": null,
"showOnMainMenu": null,
"slug": "member-listing",
"template": "member-listing",
"updated_at": "2021-09-28T09:29:56.383Z",
},
Object {
"commitments": null,
"content_sub_menu": null,
"created_at": "2021-05-19T21:33:28.500Z",
"id": 22,
"isActive": true,
"locale": "sq",
"name": "Evaluation Process / AL",
"showOnFooterMenu": null,
"showOnMainMenu": null,
"slug": "evaluation-process",
"template": "join-now",
"updated_at": "2021-05-19T21:33:29.331Z",
},
],
"post_categories": Array [
Object {
"created_at": "2021-05-20T15:54:36.949Z",
"id": 1,
"locale": "sq",
"name": "Lajmet",
"published_at": "2021-05-20T15:54:45.222Z",
"updated_at": "2021-05-20T15:54:46.179Z",
},
],
"published_at": "2021-05-13T18:34:17.260Z",
"slider": Array [
Object {
"fixed": null,
"id": 6,
"image": Array [
Object {
"alternativeText": "",
"caption": "",
"created_at": "2021-05-13T18:34:10.259Z",
"ext": ".svg",
"formats": null,
"hash": "feature_image_297dbc6cd8",
"height": 450,
"id": 11,
"mime": "image/svg+xml",
"name": "feature-image.svg",
"previewUrl": null,
"provider": "local",
"provider_metadata": null,
"size": 54.94,
"updated_at": "2021-05-13T18:34:10.522Z",
"url": "/uploads/feature_image_297dbc6cd8.svg",
"width": 600,
},
],
"parallax": null,
},
],
"sort": null,
"subTitle": null,
"thumbnail": null,
"title": "Our Vision, Mission and Guiding Principles",
"updated_at": "2021-06-22T09:27:39.155Z",
},
Object {
"category": null,
"content": "It is a quick and easy way for self-evaluating an organization in relation to the Global Standard for CSO Accountability, aiming at identifying strengths and areas for improvement.
<img src=\"/uploads/logo_2_319de9613c.jpeg\" style=\"width: 100px; height: 150px\" />
Legacy giving requires thought, lots of time and technical skills. So how do you make it as easy as possible for your donors to consider pledging a gift in a will? What decision science nudges can you use to help them along the decision-making process?
In this session you will learn the fundamentals of decision science, how people make decisions and which nudges you can use in your gifts in wills marketing to transform your appeals and secure pledges.
Learning Outcomes
1) Understand how designing legacy marketing differs from other fundraising materials
2) Learn how to leverage decision science nudges to inspire donors to consider leaving a gift in a will
3) How to write impactful legacy marketing",
"content_preview": null,
"coverImageSubtitle": null,
"coverImageTitle": null,
"created_at": "2021-05-25T21:21:03.979Z",
"custom_link": null,
"gallery": Array [],
"id": 8,
"isDeleted": null,
"locale": "sq",
"localizations": Array [],
"pages": Array [],
"post_categories": Array [],
"published_at": "2021-05-25T21:22:12.934Z",
"slider": Array [],
"sort": null,
"subTitle": "Përparimi i karrierës suaj në një botë post pandemike",
"thumbnail": null,
"title": "Publikim CSO",
"updated_at": "2021-05-25T21:22:13.300Z",
},
Object {
"category": 2,
"content": "For 50 years, AFP has been the standard-bearer for professionalism in fundraising. Learn more about AFP, its activities and people, and how you can be involved.
For 50 years, AFP has been the standard-bearer for professionalism in fundraising. Learn more about AFP, its activities and people, and how you can be involved.
For 50 years, AFP has been the standard-bearer for professionalism in fundraising. Learn more about AFP, its activities and people, and how you can be involved.
For 50 years, AFP has been the standard-bearer for professionalism in fundraising. Learn more about AFP, its activities and people, and how you can be involved.
For 50 years, AFP has been the standard-bearer for professionalism in fundraising. Learn more about AFP, its activities and people, and how you can be involved.
For 50 years, AFP has been the standard-bearer for professionalism in fundraising. Learn more about AFP, its activities and people, and how you can be involved.
For 50 years, AFP has been the standard-bearer for professionalism in fundraising. Learn more about AFP, its activities and people, and how you can be involved.
- [CSO Report 2020-2021](/)
- [CSO Report 2020-2021](/)",
"content_preview": null,
"coverImageSubtitle": "adfasdf asdf asdf asdfsdf asdf ahsfahs dfhsdhha hhhahsd fhasd fhas dfhsahdf ahsdf ashdf hasdhf has dfh asdfhashdf. h sda fashdf hasd fhashd f ashdf hashdf ",
"coverImageTitle": "Learn everything About CSO",
"created_at": "2021-05-15T08:55:42.292Z",
"custom_link": null,
"gallery": Array [],
"id": 2,
"isDeleted": null,
"locale": "sq",
"localizations": Array [],
"pages": Array [],
"post_categories": Array [],
"published_at": "2021-05-15T08:55:42.292Z",
"slider": Array [
Object {
"fixed": true,
"id": 7,
"image": Array [
Object {
"alternativeText": "",
"caption": "",
"created_at": "2021-05-13T18:34:10.259Z",
"ext": ".svg",
"formats": null,
"hash": "feature_image_297dbc6cd8",
"height": 450,
"id": 11,
"mime": "image/svg+xml",
"name": "feature-image.svg",
"previewUrl": null,
"provider": "local",
"provider_metadata": null,
"size": 54.94,
"updated_at": "2021-05-13T18:34:10.522Z",
"url": "/uploads/feature_image_297dbc6cd8.svg",
"width": 600,
},
],
"parallax": null,
},
],
"sort": null,
"subTitle": "subtitle goes here",
"thumbnail": null,
"title": "Our Vision, Mission and Guiding Principles",
"updated_at": "2021-06-22T09:27:13.855Z",
},
Object {
"category": null,
"content": "",
"content_preview": "A jeni gati për t`u bërë anëtar i Kodit? <br/>
Testoni Standardet aktuale të organizatës tuaj [këtu](/evaluation).",
"coverImageSubtitle": null,
"coverImageTitle": null,
"created_at": "2021-07-30T15:37:09.909Z",
"custom_link": "/join-now",
"gallery": Array [],
"id": 58,
"isDeleted": null,
"locale": "sq",
"localizations": Array [
Object {
"id": 61,
"locale": "en",
"published_at": "2021-09-06T08:12:34.668Z",
},
],
"pages": Array [
Object {
"commitments": null,
"content_sub_menu": 2,
"created_at": "2021-05-19T13:17:54.278Z",
"id": 10,
"isActive": true,
"locale": "sq",
"name": "Rreth Kodit",
"showOnFooterMenu": null,
"showOnMainMenu": true,
"slug": "about",
"template": "about",
"updated_at": "2021-07-28T09:43:03.711Z",
},
],
"post_categories": Array [],
"published_at": "2021-07-30T15:37:25.717Z",
"slider": Array [
Object {
"fixed": false,
"id": 99,
"image": Array [
Object {
"alternativeText": "",
"caption": "",
"created_at": "2021-06-22T11:21:15.147Z",
"ext": ".1&auto=format&fit=crop&w=1350&q=80",
"formats": Object {
"large": Object {
"ext": ".1&auto=format&fit=crop&w=1350&q=80",
"hash": "large_photo_1524178232363_1fb2b075b655_ixid_Mnwx_Mj_A3f_DB_8_M_Hxwa_G90by1w_Y_Wdlf_Hx8f_G_Vuf_DB_8f_Hx8_and_ixlib_rb_1_2_df5889d144",
"height": 667,
"mime": "image/jpeg",
"name": "large_https://images.unsplash.com/photo-1524178232363-1fb2b075b655?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1350&q=80",
"path": null,
"size": 93.66,
"url": "/uploads/large_photo_1524178232363_1fb2b075b655_ixid_Mnwx_Mj_A3f_DB_8_M_Hxwa_G90by1w_Y_Wdlf_Hx8f_G_Vuf_DB_8f_Hx8_and_ixlib_rb_1_2_df5889d144.1&auto=format&fit=crop&w=1350&q=80",
"width": 1000,
},
"medium": Object {
"ext": ".1&auto=format&fit=crop&w=1350&q=80",
"hash": "medium_photo_1524178232363_1fb2b075b655_ixid_Mnwx_Mj_A3f_DB_8_M_Hxwa_G90by1w_Y_Wdlf_Hx8f_G_Vuf_DB_8f_Hx8_and_ixlib_rb_1_2_df5889d144",
"height": 500,
"mime": "image/jpeg",
"name": "medium_https://images.unsplash.com/photo-1524178232363-1fb2b075b655?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1350&q=80",
"path": null,
"size": 59.18,
"url": "/uploads/medium_photo_1524178232...(truncated to the first 10000 characters)
How can i go inside post_categories that i haven't figured out.
Here is the code I've done so far :
const [results, setResults] = useState([]);
const [errorMessage, setErrorMessage] = useState('');
const [category, setCategory] = useState(null);
const [filteredData, setFilteredData] = useState([]);
const filteredResults = useMemo(() => results.filter(result => category === null || result.post_categories[0].id === category), [results, category]);
const toggleFilter = cat => {
setCategory(c => cat === c ? null : cat);
setResults(results)
}
This shows an undefined is not an object error for result.post_categories[0].id
How can i access that id, inside post_categories so i can filter the results afterwards?
Right now in front page it shows all the posts. After a button is clicked the toggleFilter function is triggered and it should show only the posts with that category.
From the first array after aplying the filter the desired output should go to this array :
Array [
Object {
"category": 1,
"content": "For 50 years, AFP has been the standard-bearer for professionalism in fundraising. Learn more about AFP, its activities and people, and how you can be involved.
[Link1](https://cso-web.herokuapp.com)",
"content_preview": null,
"coverImageSubtitle": null,
"coverImageTitle": null,
"created_at": "2021-05-13T18:34:17.260Z",
"custom_link": null,
"gallery": Array [],
"id": 1,
"isDeleted": null,
"locale": "sq",
"localizations": Array [],
"pages": Array [
Object {
"commitments": null,
"content_sub_menu": null,
"created_at": "2021-05-19T21:19:15.101Z",
"id": 18,
"isActive": true,
"locale": "sq",
"name": "Member Listing",
"showOnFooterMenu": null,
"showOnMainMenu": null,
"slug": "member-listing",
"template": "member-listing",
"updated_at": "2021-09-28T09:29:56.383Z",
},
Object {
"commitments": null,
"content_sub_menu": null,
"created_at": "2021-05-19T21:33:28.500Z",
"id": 22,
"isActive": true,
"locale": "sq",
"name": "Evaluation Process / AL",
"showOnFooterMenu": null,
"showOnMainMenu": null,
"slug": "evaluation-process",
"template": "join-now",
"updated_at": "2021-05-19T21:33:29.331Z",
},
],
"post_categories": Array [
Object {
"created_at": "2021-05-20T15:54:36.949Z",
"id": 1,
"locale": "sq",
"name": "Lajmet",
"published_at": "2021-05-20T15:54:45.222Z",
"updated_at": "2021-05-20T15:54:46.179Z",
},
],
"published_at": "2021-05-13T18:34:17.260Z",
"slider": Array [
Object {
"fixed": null,
"id": 6,
"image": Array [
Object {
"alternativeText": "",
"caption": "",
"created_at": "2021-05-13T18:34:10.259Z",
"ext": ".svg",
"formats": null,
"hash": "feature_image_297dbc6cd8",
"height": 450,
"id": 11,
"mime": "image/svg+xml",
"name": "feature-image.svg",
"previewUrl": null,
"provider": "local",
"provider_metadata": null,
"size": 54.94,
"updated_at": "2021-05-13T18:34:10.522Z",
"url": "/uploads/feature_image_297dbc6cd8.svg",
"width": 600,
},
],
"parallax": null,
},
],
"sort": null,
"subTitle": null,
"thumbnail": null,
"title": "Our Vision, Mission and Guiding Principles",
"updated_at": "2021-06-22T09:27:39.155Z",
},
Object {
"category": 2,
"content": "test",
"content_preview": null,
"coverImageSubtitle": null,
"coverImageTitle": null,
"created_at": "2021-05-13T18:34:17.260Z",
"custom_link": null,
"gallery": Array [],
"id": 1,
"isDeleted": null,
"locale": "sq",
"localizations": Array [],
"pages": Array [
Object {
"commitments": null,
"content_sub_menu": null,
"created_at": "2021-05-19T21:19:15.101Z",
"id": 18,
"isActive": true,
"locale": "sq",
"name": "Member Listing",
"showOnFooterMenu": null,
"showOnMainMenu": null,
"slug": "member-listing",
"template": "member-listing",
"updated_at": "2021-09-28T09:29:56.383Z",
},
Object {
"commitments": null,
"content_sub_menu": null,
"created_at": "2021-05-19T21:33:28.500Z",
"id": 22,
"isActive": true,
"locale": "sq",
"name": "Evaluation Process / AL",
"showOnFooterMenu": null,
"showOnMainMenu": null,
"slug": "evaluation-process",
"template": "join-now",
"updated_at": "2021-05-19T21:33:29.331Z",
},
],
"post_categories": Array [
Object {
"created_at": "2021-05-20T15:54:36.949Z",
"id": 1,
"locale": "sq",
"name": "Lajmet",
"published_at": "2021-05-20T15:54:45.222Z",
"updated_at": "2021-05-20T15:54:46.179Z",
},
],
"published_at": "2021-05-13T18:34:17.260Z",
"slider": Array [
Object {
"fixed": null,
"id": 6,
"image": Array [
Object {
"alternativeText": "",
"caption": "",
"created_at": "2021-05-13T18:34:10.259Z",
"ext": ".svg",
"formats": null,
"hash": "feature_image_297dbc6cd8",
"height": 450,
"id": 11,
"mime": "image/svg+xml",
"name": "feature-image.svg",
"previewUrl": null,
"provider": "local",
"provider_metadata": null,
"size": 54.94,
"updated_at": "2021-05-13T18:34:10.522Z",
"url": "/uploads/feature_image_297dbc6cd8.svg",
"width": 600,
},
],
"parallax": null,
},
],
"sort": null,
"subTitle": null,
"thumbnail": null,
"title": "Our Vision, Mission and Guiding Principles",
"updated_at": "2021-06-22T09:27:39.155Z",
},
Here is the button that should filter the results :
<Button shadowless style={styles.buttonStyle} onPress={() => { toggleFilter(1) }} >
You are getting the undefined error because for few of the cases the post_categories array is empty and if u try accessing the 0th element it will throw an error. So add a null check for the array length and for id something like below
const arr = [
{
"category": 1,
"content": "For 50 years, AFP has been the standard-bearer for professionalism in fundraising. Learn more about AFP, its activities and people, and how you can be involved.",
"content_preview": null,
"coverImageSubtitle": null,
"coverImageTitle": null,
"created_at": "2021-05-13T18:34:17.260Z",
"custom_link": null,
"gallery": [],
"id": 1,
"isDeleted": null,
"locale": "sq",
"localizations": [],
"pages": [
{
"commitments": null,
"content_sub_menu": null,
"created_at": "2021-05-19T21:19:15.101Z",
"id": 18,
"isActive": true,
"locale": "sq",
"name": "Member Listing",
"showOnFooterMenu": null,
"showOnMainMenu": null,
"slug": "member-listing",
"template": "member-listing",
"updated_at": "2021-09-28T09:29:56.383Z",
},
{
"commitments": null,
"content_sub_menu": null,
"created_at": "2021-05-19T21:33:28.500Z",
"id": 22,
"isActive": true,
"locale": "sq",
"name": "Evaluation Process / AL",
"showOnFooterMenu": null,
"showOnMainMenu": null,
"slug": "evaluation-process",
"template": "join-now",
"updated_at": "2021-05-19T21:33:29.331Z",
},
],
"post_categories": [
{
"created_at": "2021-05-20T15:54:36.949Z",
"id": 1,
"locale": "sq",
"name": "Lajmet",
"published_at": "2021-05-20T15:54:45.222Z",
"updated_at": "2021-05-20T15:54:46.179Z",
},
{
"created_at": "2021-05-20T15:54:36.949Z",
"id": 2,
"locale": "sq",
"name": "Lajmet",
"published_at": "2021-05-20T15:54:45.222Z",
"updated_at": "2021-05-20T15:54:46.179Z",
},
],
"published_at": "2021-05-13T18:34:17.260Z",
"slider": [
{
"fixed": null,
"id": 6,
"image": [
{
"alternativeText": "",
"caption": "",
"created_at": "2021-05-13T18:34:10.259Z",
"ext": ".svg",
"formats": null,
"hash": "feature_image_297dbc6cd8",
"height": 450,
"id": 11,
"mime": "image/svg+xml",
"name": "feature-image.svg",
"previewUrl": null,
"provider": "local",
"provider_metadata": null,
"size": 54.94,
"updated_at": "2021-05-13T18:34:10.522Z",
"url": "/uploads/feature_image_297dbc6cd8.svg",
"width": 600,
},
],
"parallax": null,
},
],
"sort": null,
"subTitle": null,
"thumbnail": null,
"title": "Our Vision, Mission and Guiding Principles",
"updated_at": "2021-06-22T09:27:39.155Z",
}]
console.log(arr.filter(el => el.post_categories.length > 0 && el.id === el.post_categories[0]?.id))
// console.log(arr.map(el => ({...el, post_categories: el.post_categories.filter(post => post.id === el.category)})))
Is this what you are looking for?
I am wondering how to get all of the sets of numbers that come after "id": on each line of this link using Cheerio in JS:
https://kith.com/collections/mens-footwear/products/nkcw7297-100.json
Example:
[{
"id": 19437838336128,
"product_id": 2069464547456,
"title": "3",
"price": "110.00",
"sku": "12757001",
"position": 1,
"compare_at_price": "",
"fulfillment_service": "manual",
"inventory_management": "shopify",
"option1": "3",
"option2": null,
"option3": null,
"created_at": "2020-07-13T07:24:00-04:00",
"updated_at": "2020-07-14T08:07:57-04:00",
"taxable": true,
"barcode": null,
"grams": 1361,
"image_id": null,
"weight": 3.0,
"weight_unit": "lb",
"tax_code": "PC040100",
"requires_shipping": true
}, {
"id": 19437838368896,
"product_id": 2069464547456,
"title": "3.5",
"price": "110.00",
"sku": "194496645118",
"position": 2,
"compare_at_price": "",
"fulfillment_service": "manual",
"inventory_management": "shopify",
"option1": "3.5",
"option2": null,
"option3": null,
"created_at": "2020-07-13T07:24:00-04:00",
"updated_at": "2020-07-31T17:38:25-04:00",
"taxable": true,
"barcode": null,
"grams": 1361,
"image_id": null,
"weight": 3.0,
"weight_unit": "lb",
"tax_code": "PC040100",
"requires_shipping": true
},
So I'd want to be able to get the 2 variants after "id": at the start of each item. (1st is 19437838336128 & second is 19437838368896 )
You don't DOM manipulation for that. You can use fetch (use node.js polyfill);
fetch('https://kith.com/collections/mens-footwear/products/nkcw7297-100.json')
.then(res => res.json())
.then(data => {
let variantsIds = data.product.variants.map(p => p.id);
})
.catch(console.log)
I'm trying to output all of the customer id's within my JSON data. I am using ejs as my templating engine. So far I'm only able to succsefully output 1 customer id via:
<p><%= jsonOrderData.orders[0].customer.id %></p>
When I try to loop through each order I get Cannot read property 'id' of undefined
for loop to loop through
<% for (var i = 0; i < jsonOrderData.orders.length; i++) {%>
<p>Customer: <%= jsonOrderData.orders[i].customer.id %></p>
<% }; %>
If I remove the .id after customer the error then goes away. It then outputs
Customer:[object Object] 50 times which is the legth of the json data.
I don't understand why it's not working within the loop when adding .id when it works fine without the loop manually setting the index?
JSON DATA (Cut Down 2 Orders)
{
"orders": [
{
"id": 533078016054,
"email": "email#gmail.com",
"closed_at": null,
"created_at": "2018-08-10T05:03:36+01:00",
"updated_at": "2018-08-10T05:03:37+01:00",
"number": 52,
"note": "",
"token": "f4877048c08eb98180ee5fda34f978bc",
"gateway": "manual",
"test": false,
"total_price": "13.98",
"subtotal_price": "13.98",
"total_weight": 0,
"total_tax": "0.00",
"taxes_included": false,
"currency": "GBP",
"financial_status": "pending",
"confirmed": true,
"total_discounts": "0.00",
"total_line_items_price": "13.98",
"cart_token": null,
"buyer_accepts_marketing": false,
"name": "#MW1052",
"referring_site": null,
"landing_site": null,
"cancelled_at": null,
"cancel_reason": null,
"total_price_usd": "18.00",
"checkout_token": null,
"reference": null,
"user_id": 1706983449,
"location_id": null,
"source_identifier": null,
"source_url": null,
"processed_at": "2018-08-10T05:03:36+01:00",
"device_id": null,
"phone": null,
"customer_locale": null,
"app_id": 1354745,
"browser_ip": null,
"landing_site_ref": null,
"order_number": 1052,
"discount_applications": [],
"discount_codes": [],
"note_attributes": [],
"payment_gateway_names": [
"manual"
],
"processing_method": "manual",
"checkout_id": null,
"source_name": "shopify_draft_order",
"fulfillment_status": null,
"tax_lines": [],
"tags": "",
"contact_email": "email#gmail.com",
"order_status_url": "https://checkout.shopify.com/1245839385/orders/f4877048c08eb98180ee5fda34f978bc/authenticate?key=redacted",
"admin_graphql_api_id": "gid://shopify/Order/redacted",
"line_items": [
{
"id": 1350736445494,
"variant_id": 8725905539126,
"title": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt",
"quantity": 1,
"price": "6.99",
"sku": "ss15",
"variant_title": "S / Bottle Green",
"vendor": "Fruit Of The Loom",
"fulfillment_service": "manual",
"product_id": 719146287158,
"requires_shipping": true,
"taxable": false,
"gift_card": false,
"name": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt - S / Bottle Green",
"variant_inventory_management": "shopify",
"properties": [],
"product_exists": true,
"fulfillable_quantity": 1,
"grams": 0,
"total_discount": "0.00",
"fulfillment_status": null,
"discount_allocations": [],
"admin_graphql_api_id": "gid://shopify/LineItem/1350736445494",
"tax_lines": [
{
"title": "VAT",
"price": "0.00",
"rate": 0.2
}
]
},
{
"id": 1350736478262,
"variant_id": 8725905440822,
"title": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt",
"quantity": 1,
"price": "6.99",
"sku": "ss12",
"variant_title": "S / Heather Grey",
"vendor": "Fruit Of The Loom",
"fulfillment_service": "manual",
"product_id": 719146287158,
"requires_shipping": true,
"taxable": false,
"gift_card": false,
"name": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt - S / Heather Grey",
"variant_inventory_management": "shopify",
"properties": [],
"product_exists": true,
"fulfillable_quantity": 1,
"grams": 0,
"total_discount": "0.00",
"fulfillment_status": null,
"discount_allocations": [],
"admin_graphql_api_id": "gid://shopify/LineItem/1350736478262",
"tax_lines": [
{
"title": "VAT",
"price": "0.00",
"rate": 0.2
}
]
}
],
"shipping_lines": [],
"billing_address": {
"first_name": "John",
"address1": "17A Oaklands Business Centre",
"phone": null,
"city": "Worthing",
"zip": "BN11 5LH",
"province": null,
"country": "United Kingdom",
"last_name": "Doe",
"address2": "Elm Grove",
"company": null,
"latitude": 50.8162744,
"longitude": -0.4010653,
"name": "Jogn Doe",
"country_code": "GB",
"province_code": null
},
"shipping_address": {
"first_name": "John",
"address1": "17A Oaklands Business Centre",
"phone": null,
"city": "Worthing",
"zip": "BN11 5LH",
"province": null,
"country": "United Kingdom",
"last_name": "Doe",
"address2": "Elm Grove",
"company": null,
"latitude": 50.8162744,
"longitude": -0.4010653,
"name": "John Doe",
"country_code": "GB",
"province_code": null
},
"fulfillments": [],
"refunds": [],
"customer": {
"id": 556974014518,
"email": "email#gmail.com",
"accepts_marketing": false,
"created_at": "2018-06-26T00:26:55+01:00",
"updated_at": "2018-08-10T05:03:36+01:00",
"first_name": "John",
"last_name": "Doe",
"orders_count": 22,
"state": "enabled",
"total_spent": "0.00",
"last_order_id": 533078016054,
"note": null,
"verified_email": true,
"multipass_identifier": null,
"tax_exempt": false,
"phone": null,
"tags": "",
"last_order_name": "#MW1052",
"admin_graphql_api_id": "gid://shopify/Customer/556974014518",
"default_address": {
"id": 601657278518,
"customer_id": 556974014518,
"first_name": "John",
"last_name": "Doe",
"company": null,
"address1": "17A Oaklands Business Centre",
"address2": "Elm Grove",
"city": "Worthing",
"province": null,
"country": "United Kingdom",
"zip": "BN11 5LH",
"phone": null,
"name": "John Doe",
"province_code": null,
"country_code": "GB",
"country_name": "United Kingdom",
"default": true
}
}
},
{
"id": 532977778742,
"email": "james#bungeedesign.com",
"closed_at": null,
"created_at": "2018-08-09T22:18:53+01:00",
"updated_at": "2018-08-09T22:18:53+01:00",
"number": 51,
"note": "",
"token": "a292d75bd7011cf255a1bf236b23d0a5",
"gateway": "manual",
"test": false,
"total_price": "6.99",
"subtotal_price": "6.99",
"total_weight": 0,
"total_tax": "0.00",
"taxes_included": false,
"currency": "GBP",
"financial_status": "pending",
"confirmed": true,
"total_discounts": "0.00",
"total_line_items_price": "6.99",
"cart_token": null,
"buyer_accepts_marketing": true,
"name": "#MW1051",
"referring_site": null,
"landing_site": null,
"cancelled_at": null,
"cancel_reason": null,
"total_price_usd": "9.00",
"checkout_token": null,
"reference": null,
"user_id": 1706983449,
"location_id": 1327759385,
"source_identifier": null,
"source_url": null,
"processed_at": "2018-08-09T22:18:53+01:00",
"device_id": null,
"phone": null,
"customer_locale": null,
"app_id": 1354745,
"browser_ip": null,
"landing_site_ref": null,
"order_number": 1051,
"discount_applications": [],
"discount_codes": [],
"note_attributes": [],
"payment_gateway_names": [
"manual"
],
"processing_method": "manual",
"checkout_id": null,
"source_name": "shopify_draft_order",
"fulfillment_status": null,
"tax_lines": [],
"tags": "",
"contact_email": "james#bungeedesign.com",
"order_status_url": "https://checkout.shopify.com/1245839385/orders/a292d75bd7011cf255a1bf236b23d0a5/authenticate?key=9322877ce6ce34be2feeb127d73d0f89",
"admin_graphql_api_id": "gid://shopify/Order/532977778742",
"line_items": [
{
"id": 1350552453174,
"variant_id": 8725905408054,
"title": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt",
"quantity": 1,
"price": "6.99",
"sku": "ss11",
"variant_title": "S / Black",
"vendor": "Fruit Of The Loom",
"fulfillment_service": "manual",
"product_id": 719146287158,
"requires_shipping": true,
"taxable": false,
"gift_card": false,
"name": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt - S / Black",
"variant_inventory_management": "shopify",
"properties": [],
"product_exists": true,
"fulfillable_quantity": 1,
"grams": 0,
"total_discount": "0.00",
"fulfillment_status": null,
"discount_allocations": [],
"admin_graphql_api_id": "gid://shopify/LineItem/1350552453174",
"tax_lines": [
{
"title": "VAT",
"price": "0.00",
"rate": 0.2
}
]
}
],
"shipping_lines": [],
"fulfillments": [],
"refunds": [],
"customer": {
"id": 552537620534,
"email": "james#bungeedesign.com",
"accepts_marketing": true,
"created_at": "2018-06-15T10:44:13+01:00",
"updated_at": "2018-08-09T22:18:53+01:00",
"first_name": "James",
"last_name": "Rogers",
"orders_count": 18,
"state": "enabled",
"total_spent": "0.00",
"last_order_id": 532977778742,
"note": null,
"verified_email": true,
"multipass_identifier": null,
"tax_exempt": false,
"phone": null,
"tags": "password page, prospect",
"last_order_name": "#MW1051",
"admin_graphql_api_id": "gid://shopify/Customer/552537620534"
}
}
]
}
What you have runs fine. It might be possible that something is getting lost in translation or .id is at another level. My guess is that you are not setting the variable jsonOrderData as you are expecting.
If you do console.log(jsonOrderData); do you get what you expect?
This works fine:
const json = {
"orders": [
{
"id": 533078016054,
"email": "email#gmail.com",
"closed_at": null,
"created_at": "2018-08-10T05:03:36+01:00",
"updated_at": "2018-08-10T05:03:37+01:00",
"number": 52,
"note": "",
"token": "f4877048c08eb98180ee5fda34f978bc",
"gateway": "manual",
"test": false,
"total_price": "13.98",
"subtotal_price": "13.98",
"total_weight": 0,
"total_tax": "0.00",
"taxes_included": false,
"currency": "GBP",
"financial_status": "pending",
"confirmed": true,
"total_discounts": "0.00",
"total_line_items_price": "13.98",
"cart_token": null,
"buyer_accepts_marketing": false,
"name": "#MW1052",
"referring_site": null,
"landing_site": null,
"cancelled_at": null,
"cancel_reason": null,
"total_price_usd": "18.00",
"checkout_token": null,
"reference": null,
"user_id": 1706983449,
"location_id": null,
"source_identifier": null,
"source_url": null,
"processed_at": "2018-08-10T05:03:36+01:00",
"device_id": null,
"phone": null,
"customer_locale": null,
"app_id": 1354745,
"browser_ip": null,
"landing_site_ref": null,
"order_number": 1052,
"discount_applications": [],
"discount_codes": [],
"note_attributes": [],
"payment_gateway_names": [
"manual"
],
"processing_method": "manual",
"checkout_id": null,
"source_name": "shopify_draft_order",
"fulfillment_status": null,
"tax_lines": [],
"tags": "",
"contact_email": "email#gmail.com",
"order_status_url": "https://checkout.shopify.com/1245839385/orders/f4877048c08eb98180ee5fda34f978bc/authenticate?key=redacted",
"admin_graphql_api_id": "gid://shopify/Order/redacted",
"line_items": [
{
"id": 1350736445494,
"variant_id": 8725905539126,
"title": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt",
"quantity": 1,
"price": "6.99",
"sku": "ss15",
"variant_title": "S / Bottle Green",
"vendor": "Fruit Of The Loom",
"fulfillment_service": "manual",
"product_id": 719146287158,
"requires_shipping": true,
"taxable": false,
"gift_card": false,
"name": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt - S / Bottle Green",
"variant_inventory_management": "shopify",
"properties": [],
"product_exists": true,
"fulfillable_quantity": 1,
"grams": 0,
"total_discount": "0.00",
"fulfillment_status": null,
"discount_allocations": [],
"admin_graphql_api_id": "gid://shopify/LineItem/1350736445494",
"tax_lines": [
{
"title": "VAT",
"price": "0.00",
"rate": 0.2
}
]
},
{
"id": 1350736478262,
"variant_id": 8725905440822,
"title": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt",
"quantity": 1,
"price": "6.99",
"sku": "ss12",
"variant_title": "S / Heather Grey",
"vendor": "Fruit Of The Loom",
"fulfillment_service": "manual",
"product_id": 719146287158,
"requires_shipping": true,
"taxable": false,
"gift_card": false,
"name": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt - S / Heather Grey",
"variant_inventory_management": "shopify",
"properties": [],
"product_exists": true,
"fulfillable_quantity": 1,
"grams": 0,
"total_discount": "0.00",
"fulfillment_status": null,
"discount_allocations": [],
"admin_graphql_api_id": "gid://shopify/LineItem/1350736478262",
"tax_lines": [
{
"title": "VAT",
"price": "0.00",
"rate": 0.2
}
]
}
],
"shipping_lines": [],
"billing_address": {
"first_name": "John",
"address1": "17A Oaklands Business Centre",
"phone": null,
"city": "Worthing",
"zip": "BN11 5LH",
"province": null,
"country": "United Kingdom",
"last_name": "Doe",
"address2": "Elm Grove",
"company": null,
"latitude": 50.8162744,
"longitude": -0.4010653,
"name": "Jogn Doe",
"country_code": "GB",
"province_code": null
},
"shipping_address": {
"first_name": "John",
"address1": "17A Oaklands Business Centre",
"phone": null,
"city": "Worthing",
"zip": "BN11 5LH",
"province": null,
"country": "United Kingdom",
"last_name": "Doe",
"address2": "Elm Grove",
"company": null,
"latitude": 50.8162744,
"longitude": -0.4010653,
"name": "John Doe",
"country_code": "GB",
"province_code": null
},
"fulfillments": [],
"refunds": [],
"customer": {
"id": 556974014518,
"email": "email#gmail.com",
"accepts_marketing": false,
"created_at": "2018-06-26T00:26:55+01:00",
"updated_at": "2018-08-10T05:03:36+01:00",
"first_name": "John",
"last_name": "Doe",
"orders_count": 22,
"state": "enabled",
"total_spent": "0.00",
"last_order_id": 533078016054,
"note": null,
"verified_email": true,
"multipass_identifier": null,
"tax_exempt": false,
"phone": null,
"tags": "",
"last_order_name": "#MW1052",
"admin_graphql_api_id": "gid://shopify/Customer/556974014518",
"default_address": {
"id": 601657278518,
"customer_id": 556974014518,
"first_name": "John",
"last_name": "Doe",
"company": null,
"address1": "17A Oaklands Business Centre",
"address2": "Elm Grove",
"city": "Worthing",
"province": null,
"country": "United Kingdom",
"zip": "BN11 5LH",
"phone": null,
"name": "John Doe",
"province_code": null,
"country_code": "GB",
"country_name": "United Kingdom",
"default": true
}
}
},
{
"id": 532977778742,
"email": "james#bungeedesign.com",
"closed_at": null,
"created_at": "2018-08-09T22:18:53+01:00",
"updated_at": "2018-08-09T22:18:53+01:00",
"number": 51,
"note": "",
"token": "a292d75bd7011cf255a1bf236b23d0a5",
"gateway": "manual",
"test": false,
"total_price": "6.99",
"subtotal_price": "6.99",
"total_weight": 0,
"total_tax": "0.00",
"taxes_included": false,
"currency": "GBP",
"financial_status": "pending",
"confirmed": true,
"total_discounts": "0.00",
"total_line_items_price": "6.99",
"cart_token": null,
"buyer_accepts_marketing": true,
"name": "#MW1051",
"referring_site": null,
"landing_site": null,
"cancelled_at": null,
"cancel_reason": null,
"total_price_usd": "9.00",
"checkout_token": null,
"reference": null,
"user_id": 1706983449,
"location_id": 1327759385,
"source_identifier": null,
"source_url": null,
"processed_at": "2018-08-09T22:18:53+01:00",
"device_id": null,
"phone": null,
"customer_locale": null,
"app_id": 1354745,
"browser_ip": null,
"landing_site_ref": null,
"order_number": 1051,
"discount_applications": [],
"discount_codes": [],
"note_attributes": [],
"payment_gateway_names": [
"manual"
],
"processing_method": "manual",
"checkout_id": null,
"source_name": "shopify_draft_order",
"fulfillment_status": null,
"tax_lines": [],
"tags": "",
"contact_email": "james#bungeedesign.com",
"order_status_url": "https://checkout.shopify.com/1245839385/orders/a292d75bd7011cf255a1bf236b23d0a5/authenticate?key=9322877ce6ce34be2feeb127d73d0f89",
"admin_graphql_api_id": "gid://shopify/Order/532977778742",
"line_items": [
{
"id": 1350552453174,
"variant_id": 8725905408054,
"title": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt",
"quantity": 1,
"price": "6.99",
"sku": "ss11",
"variant_title": "S / Black",
"vendor": "Fruit Of The Loom",
"fulfillment_service": "manual",
"product_id": 719146287158,
"requires_shipping": true,
"taxable": false,
"gift_card": false,
"name": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt - S / Black",
"variant_inventory_management": "shopify",
"properties": [],
"product_exists": true,
"fulfillable_quantity": 1,
"grams": 0,
"total_discount": "0.00",
"fulfillment_status": null,
"discount_allocations": [],
"admin_graphql_api_id": "gid://shopify/LineItem/1350552453174",
"tax_lines": [
{
"title": "VAT",
"price": "0.00",
"rate": 0.2
}
]
}
],
"shipping_lines": [],
"fulfillments": [],
"refunds": [],
"customer": {
"id": 552537620534,
"email": "james#bungeedesign.com",
"accepts_marketing": true,
"created_at": "2018-06-15T10:44:13+01:00",
"updated_at": "2018-08-09T22:18:53+01:00",
"first_name": "James",
"last_name": "Rogers",
"orders_count": 18,
"state": "enabled",
"total_spent": "0.00",
"last_order_id": 532977778742,
"note": null,
"verified_email": true,
"multipass_identifier": null,
"tax_exempt": false,
"phone": null,
"tags": "password page, prospect",
"last_order_name": "#MW1051",
"admin_graphql_api_id": "gid://shopify/Customer/552537620534"
}
}
]
};
for (let i = 0; i < json.orders.length; i++) {
const order = json.orders[i];
console.log(order.customer.id);
}