How could I "Turn inside out" a JSON object received from a server API using javascript?
example input
[
{
"id": 7,
"idAsignacion": 9,
"idPregunta": 4,
"cumplimiento": 1,
"observacionNumeral": 20,
"observacionEscrita": "HOLAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
"rutaObservacionDocumental": "1/1/1/1/4",
"created_at": "2017-01-31 18:26:44",
"updated_at": "2017-01-31 18:26:44",
"traer_preguntas": {
"id": 4,
"idRequisito": 1,
"ordenPreguntas": 1,
"pregunta": "jojgpofdkñkñdkgñk",
"tecnicaAuditoria": "Observación",
"escrita": 1,
"numeral": 1,
"documental": 1,
"estado": 0,
"created_at": "2017-01-31 15:42:41",
"updated_at": "2017-01-31 15:42:41",
"obtener_requisitos": {
"id": 1,
"ordenRequisito": 1,
"idDimension": 1,
"nombreRequisito": "Requisito uno",
"estado": 0,
"created_at": "2017-01-30 15:19:02",
"updated_at": "2017-01-30 15:19:02",
"obtener_dimensiones": {
"id": 1,
"ordenDimension": 1,
"dimension": "Dimension UNO",
"estado": 0,
"created_at": "2017-01-30 15:18:48",
"updated_at": "2017-01-30 15:18:48"
}
}
}
}
]
desired output
"obtener_dimensiones": {
"id": 1,
"ordenDimension": 1,
"dimension": "Dimension UNO",
"estado": 0,
"created_at": "2017-01-30 15:18:48",
"updated_at": "2017-01-30 15:18:48""obtener_requisitos": {
"id": 1,
"ordenRequisito": 1,
"idDimension": 1,
"nombreRequisito": "Requisito uno",
"estado": 0,
"created_at": "2017-01-30 15:19:02",
"updated_at": "2017-01-30 15:19:02",
"traer_preguntas": {
"id": 4,
"idRequisito": 1,
"ordenPreguntas": 1,
"pregunta": "jojgpofdkñkñdkgñk",
"tecnicaAuditoria": "Observación",
"escrita": 1,
"numeral": 1,
"documental": 1,
"estado": 0,
"created_at": "2017-01-31 15:42:41",
"updated_at": "2017-01-31 15:42:41",
{
"id": 7,
"idAsignacion": 9,
"idPregunta": 4,
"cumplimiento": 1,
"observacionNumeral": 20,
"observacionEscrita": "HOLAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
"rutaObservacionDocumental": "1/1/1/1/4",
"created_at": "2017-01-31 18:26:44",
"updated_at": "2017-01-31 18:26:44",
}
}
}
}
How to turn the last element to be the first and so on until every "child" property becomes the parent of it's original parent?
Input:
obj = [{
"id": 7,
"idAsignacion": 9,
"idPregunta": 4,
"cumplimiento": 1,
"observacionNumeral": 20,
"observacionEscrita": "HOLAAAAAAAAAAAAAAAAAAAA",
"rutaObservacionDocumental": "1/1/1/1/4",
"created_at": "2017-01-31 18:26:44",
"updated_at": "2017-01-31 18:26:44",
"traer_preguntas": {
"id": 4,
"idRequisito": 1,
"ordenPreguntas": 1,
"pregunta": "jojgpofdkñkñdkgñk",
"tecnicaAuditoria": "Observación",
"escrita": 1,
"numeral": 1,
"documental": 1,
"estado": 0,
"created_at": "2017-01-31 15:42:41",
"updated_at": "2017-01-31 15:42:41",
"obtener_requisitos": {
"id": 1,
"ordenRequisito": 1,
"idDimension": 1,
"nombreRequisito": "Requisito uno",
"estado": 0,
"created_at": "2017-01-30 15:19:02",
"updated_at": "2017-01-30 15:19:02",
"obtener_dimensiones": {
"id": 1,
"ordenDimension": 1,
"dimension": "Dimension UNO",
"estado": 0,
"created_at": "2017-01-30 15:18:48",
"updated_at": "2017-01-30 15:18:48"
}
}
}
}];
The Code:
rd(obj[0])
newObj = {}
kn1 = Object.keys(th[2])
newObj[kn1] = th[2][kn1]
kn2 = Object.keys(th[1])
newObj[kn1][kn2] = th[1][kn2]
kn3 = Object.keys(th[0])
newObj[kn1][kn2][kn3] = th[0][kn3]
newObj[kn1][kn2][kn3].root = thr
console.log(JSON.stringify([newObj], null, 4));
function rd(mo, od) {
if (typeof(od) === "undefined") {th = {}; od = 0}
else {od++}
Object.keys(mo).forEach(function(k) {
if (typeof(mo[k]) === "object") {
console.group("depth=" + od + " : " + k + "(" + Object.keys(mo[k]).length + ")");
rd(mo[k], od)
console.groupEnd();
if (typeof(th[od]) === "undefined") th[od] = {}
th[od][k] = mo[k]
Object.keys(th[od][k]).forEach(function(ik) {
if (typeof(th[od][k][ik]) === "object") {
delete th[od][k][ik]
}
})
} else {
console.log("depth=" + od + " : " + k)
if (od === 0) {
if (typeof(thr) === "undefined") thr = {}
thr[k] = mo[k]
}
}
});
}
the 2nd part of the code ca be much improved, I can work on it if needed.
I added the Key Name root to make the JSON valid
Output:
[
{
"obtener_dimensiones": {
"id": 1,
"ordenDimension": 1,
"dimension": "Dimension UNO",
"estado": 0,
"created_at": "2017-01-30 15:18:48",
"updated_at": "2017-01-30 15:18:48",
"obtener_requisitos": {
"id": 1,
"ordenRequisito": 1,
"idDimension": 1,
"nombreRequisito": "Requisito uno",
"estado": 0,
"created_at": "2017-01-30 15:19:02",
"updated_at": "2017-01-30 15:19:02",
"traer_preguntas": {
"id": 4,
"idRequisito": 1,
"ordenPreguntas": 1,
"pregunta": "jojgpofdkñkñdkgñk",
"tecnicaAuditoria": "Observación",
"escrita": 1,
"numeral": 1,
"documental": 1,
"estado": 0,
"created_at": "2017-01-31 15:42:41",
"updated_at": "2017-01-31 15:42:41",
"root": {
"id": 7,
"idAsignacion": 9,
"idPregunta": 4,
"cumplimiento": 1,
"observacionNumeral": 20,
"observacionEscrita": "HOLAAAAAAAAAAAAAAAAAAAA",
"rutaObservacionDocumental": "1/1/1/1/4",
"created_at": "2017-01-31 18:26:44",
"updated_at": "2017-01-31 18:26:44"
}
}
}
}
}
]
Related
The object
{
"Home work": [
{
"id": 5,
"student_id": 2,
"activity_id": 3,
"checked": 1,
"checked_at": "2023-02-07 06:03:06",
"completed": 1,
"created_at": "2023-02-06T12:31:22.000000Z",
"updated_at": "2023-02-07T06:03:11.000000Z",
"activity_category_name": "Home work"
},
{
"id": 2,
"student_id": 2,
"activity_id": 2,
"checked": 1,
"checked_at": "2023-02-07 06:03:38",
"completed": 0,
"created_at": "2023-02-06T10:44:31.000000Z",
"updated_at": "2023-02-07T06:03:38.000000Z",
"activity_category_name": "Home work"
}
],
"Daily activity": [
{
"id": 1,
"student_id": 2,
"activity_id": 1,
"checked": 1,
"checked_at": "2023-02-05 11:18:56",
"completed": 1,
"created_at": "2023-02-05T10:40:25.000000Z",
"updated_at": "2023-02-05T11:18:56.000000Z",
"activity_category_name": "Daily activity"
}
]}
Here i want the index number of Home work which will be 0.
I tries
for((val, index) in array)
{
console.log(val) // output 'Home work : [...]'
console.log(index) // output 'Home work'
}
I need to find the index number here
Thanks
Try this code
var json = {
"Home work": [
{
"id": 5,
"student_id": 2,
"activity_id": 3,
"checked": 1,
"checked_at": "2023-02-07 06:03:06",
"completed": 1,
"created_at": "2023-02-06T12:31:22.000000Z",
"updated_at": "2023-02-07T06:03:11.000000Z",
"activity_category_name": "Home work"
},
{
"id": 2,
"student_id": 2,
"activity_id": 2,
"checked": 1,
"checked_at": "2023-02-07 06:03:38",
"completed": 0,
"created_at": "2023-02-06T10:44:31.000000Z",
"updated_at": "2023-02-07T06:03:38.000000Z",
"activity_category_name": "Home work"
}
],
"Daily activity": [
{
"id": 1,
"student_id": 2,
"activity_id": 1,
"checked": 1,
"checked_at": "2023-02-05 11:18:56",
"completed": 1,
"created_at": "2023-02-05T10:40:25.000000Z",
"updated_at": "2023-02-05T11:18:56.000000Z",
"activity_category_name": "Daily activity"
}
]
};
var keys = Object.keys(json);
for (const key of keys) {
console.log(key);
var result = keys.indexOf(key);
console.log(result);
}
Since it is an Object, We cannot access its key "Home Work" as index 0 but you can try Object.keys(yourObject) to access all of its keys.
You can get the index numbers of keys of an object just by using the Object.keys() method, which returns a list of all the keys of the object. Then, you use forEach method to iterate over the list of keys and get the index of each key.
Example:
var yourObject = {
"Home work": [
{
"id": 5,
"student_id": 2,
"activity_id": 3,
"checked": 1,
"checked_at": "2023-02-07 06:03:06",
"completed": 1,
"created_at": "2023-02-06T12:31:22.000000Z",
"updated_at": "2023-02-07T06:03:11.000000Z",
"activity_category_name": "Home work"
},
{
"id": 2,
"student_id": 2,
"activity_id": 2,
"checked": 1,
"checked_at": "2023-02-07 06:03:38",
"completed": 0,
"created_at": "2023-02-06T10:44:31.000000Z",
"updated_at": "2023-02-07T06:03:38.000000Z",
"activity_category_name": "Home work"
}
],
"Daily activity": [
{
"id": 1,
"student_id": 2,
"activity_id": 1,
"checked": 1,
"checked_at": "2023-02-05 11:18:56",
"completed": 1,
"created_at": "2023-02-05T10:40:25.000000Z",
"updated_at": "2023-02-05T11:18:56.000000Z",
"activity_category_name": "Daily activity"
}
]}
To get both keys and their index, use:
Object.keys(yourObject ).forEach((key, index) => {
console.log(`Key:: ${key}, index:: ${index}`)
})
If you want to get only the indexes of the keys, use:
Object.keys(yourObject ).forEach((key, index) => {
console.log(index)
})
my friend, I hope you are all fine over there. Please, I would like to know how I can access some specific value within an array object! I am designing an app with Redux, and I was able to retrieve the first array object value but the last array object which is more complex than the previous is giving headache. Can someone help me out. Below is the Object:
{
[
[{
"id": 1,
"full_name": "jack",
"email": "carl#gmail.com",
"phone": "2123309",
"dob": "2008-06-12",
"location": "VAUGHAN",
"user_id": 1,
"created_at": "2021-10-02T09:10:04.000000Z",
"updated_at": "2021-10-02T12:16:57.000000Z"
}],
[{
"id": 1,
"price": "432.00",
"order_type": "Car Parking",
"currency": "USD",
"paid": 0,
"amount_paid": "432.00",
"overdue": "0.00",
"client_id": 1,
"user_id": 1,
"created_at": "2021-10-02T09:10:26.000000Z",
"updated_at": "2021-10-02T09:46:00.000000Z"
}, {
"id": 2,
"price": "2500.00",
"order_type": "Ramp",
"currency": "USD",
"paid": 0,
"amount_paid": "2030.00",
"overdue": "470.00",
"client_id": 1,
"user_id": 1,
"created_at": "2021-10-02T09:48:07.000000Z",
"updated_at": "2021-10-02T10:14:22.000000Z"
}, {
"id": 9,
"price": "893.00",
"order_type": "Shipping",
"currency": "CAD",
"paid": 0,
"amount_paid": "765.00",
"overdue": "128.00",
"client_id": 1,
"user_id": 1,
"created_at": "2021-10-02T10:46:45.000000Z",
"updated_at": "2021-10-02T10:54:06.000000Z"
}, {
"id": 21,
"price": "250.00",
"order_type": "Storage rent",
"currency": "USD",
"paid": 0,
"amount_paid": "0.00",
"overdue": "250.00",
"client_id": 1,
"user_id": 1,
"created_at": "2021-10-03T08:33:13.000000Z",
"updated_at": "2021-10-03T08:33:13.000000Z"
}]
]
}
To access FULL_NAME I did :
client.data.map((client) => client[0].full_name)
if I try to access a second array with [1] it gives me cannot access that property.
Now there are two things I wish to do here, get the length of the second array, and be able to loop over that array. Also, how can I access a specific value in that object, let assume for instance I want to retrieve AMOUNT_PAID in object 2. Any idea please?
You can map over the array at index [1], and use .length to get the length of the outcome array.
const data = [
[{
"id": 1,
"full_name": "jack",
"email": "carl#gmail.com",
"phone": "2123309",
"dob": "2008-06-12",
"location": "VAUGHAN",
"user_id": 1,
"created_at": "2021-10-02T09:10:04.000000Z",
"updated_at": "2021-10-02T12:16:57.000000Z"
}],
[{
"id": 1,
"price": "432.00",
"order_type": "Car Parking",
"currency": "USD",
"paid": 0,
"amount_paid": "432.00",
"overdue": "0.00",
"client_id": 1,
"user_id": 1,
"created_at": "2021-10-02T09:10:26.000000Z",
"updated_at": "2021-10-02T09:46:00.000000Z"
}, {
"id": 2,
"price": "2500.00",
"order_type": "Ramp",
"currency": "USD",
"paid": 0,
"amount_paid": "2030.00",
"overdue": "470.00",
"client_id": 1,
"user_id": 1,
"created_at": "2021-10-02T09:48:07.000000Z",
"updated_at": "2021-10-02T10:14:22.000000Z"
}, {
"id": 9,
"price": "893.00",
"order_type": "Shipping",
"currency": "CAD",
"paid": 0,
"amount_paid": "765.00",
"overdue": "128.00",
"client_id": 1,
"user_id": 1,
"created_at": "2021-10-02T10:46:45.000000Z",
"updated_at": "2021-10-02T10:54:06.000000Z"
}, {
"id": 21,
"price": "250.00",
"order_type": "Storage rent",
"currency": "USD",
"paid": 0,
"amount_paid": "0.00",
"overdue": "250.00",
"client_id": 1,
"user_id": 1,
"created_at": "2021-10-03T08:33:13.000000Z",
"updated_at": "2021-10-03T08:33:13.000000Z"
}]
]
const result = data[1].map(x => x.amount_paid);
const resultLength = result.length;
console.log("Result: ", result);
console.log("Length: ", resultLength);
I have an object like this:
"data": [
{
"type": "products",
"id": "2021-01-04.1.1",
"attributes": {
"name": "product 1",
"descriptions": "some description",
"image": null,
"date": "2021-01-04",
"valid_from": null,
"valig_untill": null,
"xparc_paid_product_id": 1,
"xparc_unpaid_product_id": 1,
"xparc_access_id": 1,
"stock": null,
"variations": [
{
"id": 1,
"product_template_id": "1",
"name": "child ticket",
"description": "Only for children!",
"price_excl_vat": 1,
"created_at": "2021-09-15T13:16:00.000000Z",
"updated_at": "2021-09-15T13:16:00.000000Z"
},
{
"id": 2,
"product_template_id": "1",
"name": "Adults",
"description": "Not for children!",
"price_excl_vat": 2,
"created_at": "2021-09-15T13:16:10.000000Z",
"updated_at": "2021-09-15T13:16:10.000000Z"
}
]
},
"links": {
"self": "..."
}
}, ...
]
So as you see data has object inside. What I am trying to do is I want to find the cheapest price (price_excl_vat) inside variations. But I don't know how I should return. I have started to create this function:
priceCalculationCheapest(obj){
let arr = Object.values(obj);
let min = Math.min(...arr);
return min;
},
But I cannot go further. Could you please help me about this?
Thanks
You mean this
const obj = {
data: {
"variations": [{
"id": 1,
"product_template_id": "1",
"name": "child ticket",
"description": "Only for children!",
"price_excl_vat": 1,
"created_at": "2021-09-15T13:16:00.000000Z",
"updated_at": "2021-09-15T13:16:00.000000Z"
},
{
"id": 2,
"product_template_id": "1",
"name": "Adults",
"description": "Not for children!",
"price_excl_vat": 2,
"created_at": "2021-09-15T13:16:10.000000Z",
"updated_at": "2021-09-15T13:16:10.000000Z"
}
]
}
}
const price = Math.min(...obj.data.variations
.map(({price_excl_vat}) => price_excl_vat ))
console.log(price)
try below code.
var obj = {
"data": [{
"type": "products",
"id": "2021-01-04.1.1",
"attributes": {
"name": "product 1",
"descriptions": "some description",
"image": null,
"date": "2021-01-04",
"valid_from": null,
"valig_untill": null,
"xparc_paid_product_id": 1,
"xparc_unpaid_product_id": 1,
"xparc_access_id": 1,
"stock": null,
"variations": [{
"id": 1,
"product_template_id": "1",
"name": "child ticket",
"description": "Only for children!",
"price_excl_vat": 1,
"created_at": "2021-09-15T13:16:00.000000Z",
"updated_at": "2021-09-15T13:16:00.000000Z"
},
{
"id": 2,
"product_template_id": "1",
"name": "Adults",
"description": "Not for children!",
"price_excl_vat": 2,
"created_at": "2021-09-15T13:16:10.000000Z",
"updated_at": "2021-09-15T13:16:10.000000Z"
}
]
},
"links": {
"self": "..."
}
}]
}
function priceCalculationCheapest(arr) {
let small = arr.reduce((first, second) => first.price_excl_vat < second.price_excl_vat ? first : second);
return small;
};
console.log(priceCalculationCheapest(obj.data[0].attributes.variations));
Best and easy way to sort array. If you have fewer records. which will be O(nlogn). Else you can use linear for loop to find min or max records.
Sample:
const obj = {
data: {
variations: [
{
id: 1,
product_template_id: "1",
name: "child ticket",
description: "Only for children!",
price_excl_vat: 1,
created_at: "2021-09-15T13:16:00.000000Z",
updated_at: "2021-09-15T13:16:00.000000Z",
},
{
id: 2,
product_template_id: "1",
name: "Adults",
description: "Not for children!",
price_excl_vat: 2,
created_at: "2021-09-15T13:16:10.000000Z",
updated_at: "2021-09-15T13:16:10.000000Z",
},
],
},
};
const sortBy = (key, data = []) => {
return data.sort((v1, v2) => {
return v1[key] - v2[key]; // for reverse, max to min v2[key]-v1[key]
});
};
const priceSorted = sortBy("product_template_id", obj.data.variations);
console.log(priceSorted[0]); // min
console.log(priceSorted[obj.data.variations.length - 1]); // max
Iterate through each object in array and compare the price to find minimum.
const product = {
data: {
"variations": [{
"id": 1,
"product_template_id": "1",
"name": "child ticket",
"description": "Only for children!",
"price_excl_vat": 1,
"created_at": "2021-09-15T13:16:00.000000Z",
"updated_at": "2021-09-15T13:16:00.000000Z"
},
{
"id": 2,
"product_template_id": "1",
"name": "Adults",
"description": "Not for children!",
"price_excl_vat": 2,
"created_at": "2021-09-15T13:16:10.000000Z",
"updated_at": "2021-09-15T13:16:10.000000Z"
}
]
}
};
const getMinPrice = (list) => {
let minPrice = list[0].price_excl_vat;
list.forEach(obj => {
if (obj.price_excl_vat < minPrice) {
minPrice = obj.price_excl_vat;
}
});
return minPrice;
}
console.log(getMinPrice(product.data.variations));
The Question;
I want to filter by properties.
I have json data coming from the api and I want to filter that data.(If there is a way to do with laravel I would like to try that too)
Json Data;
[
{
"id": 1,
"title": "Deneme Batarya",
"description": "<p>Deneme Batarya</p>",
"slug": "deneme-batarya",
"brand_id": 4,
"deleted_at": null,
"created_at": "2021-02-22 19:43:02",
"updated_at": "2021-02-22 19:43:02",
"categories": [
{
"id": 3,
"parent_id": null,
"title": "Giyilebilir Teknoloji",
"description": null,
"slug": "giyilebilir-teknoloji",
"media": "public/media/slider/ms-10.jpg",
"level": 0,
"deleted_at": null,
"created_at": null,
"updated_at": null,
"pivot": {
"product_id": 1,
"category_id": 3
}
},
{
"id": 26,
"parent_id": 3,
"title": "Akıllı Bileklik",
"description": null,
"slug": "akilli-bileklik",
"media": "public/media/slider/ms-10.jpg",
"level": 2,
"deleted_at": null,
"created_at": null,
"updated_at": null,
"pivot": {
"product_id": 1,
"category_id": 26
}
}
],
"images": [
{
"id": 3,
"product_id": 1,
"product_detail_id": 1,
"sku": "model-45b",
"image": "public/media/product/1-model-45b864.png",
"level": 1,
"deleted_at": null,
"created_at": "2021-02-24 19:49:16",
"updated_at": "2021-02-24 19:49:16"
},
{
"id": 4,
"product_id": 1,
"product_detail_id": null,
"sku": null,
"image": "public/media/product/1--501.jpeg",
"level": 1,
"deleted_at": null,
"created_at": "2021-02-24 19:53:27",
"updated_at": "2021-02-24 19:53:27"
}
],
"details": [
{
"id": 1,
"product_id": 1,
"qty": 5,
"status": "Available",
"price": "56.00",
"discount": 5,
"sku": "model-45b",
"start": "2021-02-22 00:00:00",
"end": "2021-02-23 00:00:00",
"deleted_at": null,
"created_at": "2021-02-22 19:43:27",
"updated_at": "2021-02-22 19:43:27"
},
{
"id": 2,
"product_id": 1,
"qty": 6,
"status": "Available",
"price": "90.00",
"discount": 4,
"sku": "model-45a",
"start": "2021-02-25 00:00:00",
"end": "2021-02-26 00:00:00",
"deleted_at": null,
"created_at": "2021-02-24 19:26:48",
"updated_at": "2021-02-24 19:26:48"
}
],
"properties": [
{
"id": 1,
"product_id": 1,
"product_detail_id": 1,
"property_id": 7,
"parent_id": 1,
"created_at": "2021-02-22 19:43:27",
"updated_at": "2021-02-22 19:43:27",
"property": {
"id": 7,
"parent_id": 1,
"title": "Green",
"slug": "green",
"value": "#008000",
"created_at": null,
"updated_at": null
},
"parent": {
"id": 1,
"parent_id": null,
"title": "Color",
"slug": "color",
"value": null,
"created_at": null,
"updated_at": null
}
},
{
"id": 2,
"product_id": 1,
"product_detail_id": 1,
"property_id": 20,
"parent_id": 3,
"created_at": "2021-02-22 19:43:27",
"updated_at": "2021-02-22 19:43:27",
"property": {
"id": 20,
"parent_id": 3,
"title": "1 TB",
"slug": "1-tb",
"value": "1 TB",
"created_at": null,
"updated_at": null
},
"parent": {
"id": 3,
"parent_id": null,
"title": "Store Capacity",
"slug": "store-capacity",
"value": null,
"created_at": null,
"updated_at": null
}
},
{
"id": 3,
"product_id": 1,
"product_detail_id": 2,
"property_id": 7,
"parent_id": 1,
"created_at": "2021-02-24 19:26:48",
"updated_at": "2021-02-24 19:26:48",
"property": {
"id": 7,
"parent_id": 1,
"title": "Green",
"slug": "green",
"value": "#008000",
"created_at": null,
"updated_at": null
},
"parent": {
"id": 1,
"parent_id": null,
"title": "Color",
"slug": "color",
"value": null,
"created_at": null,
"updated_at": null
}
},
{
"id": 4,
"product_id": 1,
"product_detail_id": 2,
"property_id": 12,
"parent_id": 2,
"created_at": "2021-02-24 19:26:48",
"updated_at": "2021-02-24 19:26:48",
"property": {
"id": 12,
"parent_id": 2,
"title": "L",
"slug": "l",
"value": "Large",
"created_at": null,
"updated_at": null
},
"parent": {
"id": 2,
"parent_id": null,
"title": "Size",
"slug": "size",
"value": null,
"created_at": null,
"updated_at": null
}
},
{
"id": 5,
"product_id": 1,
"product_detail_id": 2,
"property_id": 17,
"parent_id": 3,
"created_at": "2021-02-24 19:26:48",
"updated_at": "2021-02-24 19:26:48",
"property": {
"id": 17,
"parent_id": 3,
"title": "64 GB",
"slug": "64-gb",
"value": "64 GB",
"created_at": null,
"updated_at": null
},
"parent": {
"id": 3,
"parent_id": null,
"title": "Store Capacity",
"slug": "store-capacity",
"value": null,
"created_at": null,
"updated_at": null
}
}
]
}
]
I don't know the properties that user added because it is dynamic. So I'm reaching the properties like this.
function makeQueryString() {
function keyValues(idPref) {
var key = idPref,
values = $('#' + idPref + ' input[id^="' + idPref + '"]').map(function() {
if (this.value !== this.defaultValue || this.checked !== this.defaultChecked) {
return this.value;
}
}).get().join(',');
return key && values.length ? key + '=' + values : '';
}
return $('form#property-filter span[id]').map(function(){
let keyValue = keyValues(this.id);
if (keyValue != ''){
return keyValue;
}
}).get().join('&');
}
Properties Result;
id of properties with name
color=6,7&store-capacity=15,16,17&ram=21,22,23
EDIT
The main problem is make a filter using property result.
For example when user select Red And Blue color and click filter button it will return color=6,7 and I should filter my json data with this and bring only if include color 6 or 7. At the same time if user select red, 64GB, 128GB then it should return only if red color with 64 or 128.
I tried so far;
function filterProducts(prop) {
// prop = color=6,7&store-capacity=15,16,17&ram=21,22
prop = prop.split('&');
let x = [];
for (i=0; i<prop.length; i++){
x.push(prop[i].split('='));
}
let newProductsArr = [];
for (p=0; p<products.length; p++) {
for (k=0; k< products[p].properties.length; k++){
for (i=0; i<x.length; i++){
if(x[i][0] === products[p].properties[k].parent.slug){
let ids = x[i][1].split(',');
for (g=0; g<ids.length; i++){
if (products[p].properties[k].property_id === ids[g]){
newProductsArr.push(products[p])
}
}
}
}
}
}
}
I have an object which looks something like this:
{
"11": {
"id": 11,
"parent_product": 0,
"product_name": "WebStore",
"product_price_city": null,
"child": {
"12": {
"id": 12,
"parent_product": 11,
"product_name": "WebStore - Single",
"product_price_city": 500
},
"13": {
"id": 13,
"parent_product": 11,
"product_name": "WebStore - Full",
"product_price_city": 2500
}
}
}
}
Here, the key 11 has two childs: 12 and 13.
In a different object which looks like this:
{
"316":
{
"id": 316,
"product_id": 13,
"sold_by": 1,
"product_price": 5000,
"subscription_month": 3,
"updated_by": 0,
"created_at": 1449573556
},
"317":
{
"id": 317,
"product_id": 12,
"sold_by": 1,
"product_price": 5000,
"subscription_month": 3,
"updated_by": 0,
"created_at": 1449573556
}
}
I here the product_id is either 12 or 13, that is it will always be a child.
I need to get the parent id of 12 and 13, so I can access they the first object.
data.11
How can I get it in JavaScript?
Just get the properties and iterate over it.
var object1 = {
"11": { "id": 11, "parent_product": 0, "product_name": "WebStore", "product_price_city": null, "child": { "12": { "id": 12, "parent_product": 11, "product_name": "WebStore - Single", "product_price_city": 500 },
"13": { "id": 13, "parent_product": 11, "product_name": "WebStore - Full", "product_price_city": 2500 } } },
"15": { "id": 15, "parent_product": 0, "product_name": "WebStore", "product_price_city": null, "child": undefined },
"17": { "id": 17, "parent_product": 0, "product_name": "WebStore", "product_price_city": null }
},
key;
function getParent(id, object) {
var k;
Object.keys(object).some(function (a) {
if (object[a].child && id in object[a].child) {
k = a;
return true;
}
});
return k;
}
document.write(getParent('12', object1) + '<br>'); // '11'
key = getParent('42', object1);
if (typeof key === 'undefined') {
document.write('no key found');
}