Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Search for text in an array nodeJS

I have the following data

let rawdata = fs.readFileSync('test.json');
let orders = JSON.parse(rawdata);

Rawdata data looks like this

[{"order":["Awaiting delivery","Order date: Dec 18, 2022","Order ID: 11111111","Copy","Order details","store name","product name","1, 1","US $1.73x1","Total: US $5.39","Confirm receipt","Track order"]},{"order":["Awaiting delivery","Order date: Dec 18, 2022","Order ID: 2222222","Copy","Order details","store name_2","5","items","Total: US $13.07","Confirm receipt","Track order"]}]

What I’m trying to do is to extract the order’s date, and order’s total.
So far I wrote the following code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

orders.forEach(element => {
let order_date = element.order[1].replace('Order date:', '')
let order_ID = element.order[2].replace('Order ID:', '')
let store_name = element.order[5]
for (i=0; i<15; i++) {
    if (element.order[i].includes('Total')) {
        let order_total = element.order[i]
    }
}
console.log(order_date + "*" + order_ID + "*" + store_name + "*" + order_total)
});

I’m not able to extract the order’s total because I receive the error TypeError: Cannot read properties of undefined (reading 'includes')
I also tried to use .contains() but it doesn’t work either.

The expected results from the console.log would be something like this:

Dec 18, 2022*11111111*store name*Total: US $5.39
Dec 18, 2022*2222222*store name_2*Total: US $13.07

Could you please help me to understand what I’m doing wrong?

Thanks a lot in advance

>Solution :

 let rawdata = fs.readFileSync('test.json');

let orders = JSON.parse(rawdata);

orders.forEach(element => {
let order_date = element.order[1].replace('Order date:', '')
let order_ID = element.order[2].replace('Order ID:', '')
let store_name = element.order[5]
let order_total = 0
for (i=0; i<element.order.length; i++) {
    if (element.order[i].includes('Total')) {
        order_total = element.order[i]
    }
}
console.log(order_date + "*" + order_ID + "*" + store_name + "*" + order_total)    
});

length not up to 15 user element.length and just one change in order raw data it is invalid add [ so it look like

[{"order":["Awaiting delivery","Order date: Dec 18, 2022","Order ID: 11111111","Copy","Order details","store name","product name","1, 1","US $1.73×1","Total: US $5.39","Confirm receipt","Track order"]},{"order":["Awaiting delivery","Order date: Dec 18, 2022","Order ID: 2222222","Copy","Order details","store name_2","5","items","Total: US $13.07","Confirm receipt","Track order"]}]

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading