Home > Net >  Search for text in an array nodeJS
Search for text in an array nodeJS

Time:12-22

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:

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

CodePudding user response:

 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.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"]}]

CodePudding user response:

TypeError: Cannot read properties of undefined (reading 'includes') means you're trying to read element outside of the bounds of the Array. Try looping over the actual length like this :

for (i=0; i<element.order.length; i  ) {
    if (element.order[i].includes('Total')) {
        let order_total = element.order[i]
    }
}

and your error should disappear.

  • Related