I am trying to go through an number of tab-separated words in a file and check if the key is in the structure (json):
The json object looks like this:
[
{
"Name" : "Cypress Upload 1",
"Description" : "Cypress upload descrition 1"
},
{
"Name" : "Cypress Upload 2",
"Description" : "Cypress upload descrition 2"
}
]
The code looks like this:
cy.readFile(uploadPathFile).then((jData) => {
cy.log(jData[0].Name);
const objLn = Object.keys(jData).length;
cy.get('@LASTDOWNLOADEDFILE').then((downFile) => {
cy.readFile(downFile as unknown as string).then((txt) => {
const lines = (txt as string).split('t');
for (let o = 0; o < objLn; o++) {
for (let i = 0; i < lines.length; i++) {
cy.log(`Key ${lines[i]} ${jData[o].Name}`);
if (jData[o].hasOwnProperty(lines[i])) {
cy.log(`Found Key ${lines[i]}`);
}
}
}
});
});
});
The first log: gives me the correct value of the Name key at position 0
18 log Cypress Upload 1
The second log lists me all the names from the tab separated file:
...
21 log Key "ID" Cypress Upload 1
22 log Key "Name" Cypress Upload 1
23 log Key "Synonym 1" Cypress Upload 1
...
But it does not show the key found even though “Name” is there and jData[0].Name gives a value! Why is the line with “if (jData[o].hasOwnProperty(lines[i])) {
” never true. I also tried “if (jData[o][lines[i]]) {
“