There is a dataset available:
var data = {
"variants": [{
"quantity": "20",
"varientId": 8,
"currency": "YEN",
"extraField": {
"Size": "10",
"Color": "Red",
"Material": "Denim"
},
"price": "199"
},
{
"quantity": "15",
"varientId": 10,
"currency": "YEN",
"extraField": {
"Size": "9",
"Color": "Red",
"Material": "Denim"
},
"price": "249"
},
{
"quantity": "18",
"varientId": 12,
"currency": "YEN",
"extraField": {
"Size": "8",
"Color": "Green",
"Material": "Rubber",
},
"price": "279"
}
]
}
In addition to the data, an object is also provided:
var obj = {
"Size": "10",
"Color": "Red",
"Material": "Denim"
}
An attempt was made to search for the object within the dataset:
var index = null
for(var l = 0; l<data.variants.length; l++){
if(data.variants[l].extraField === obj){
index = l
}
}
console.log(index)
Another alternative method using JSON.stringify
was also tested:
JSON.stringify(data.variants[l].extraField) === JSON.stringify(obj)
The result obtained from console logging the index variable shows null instead of the expected value of 0, as the obj
object matches with the first variant's extrafield.