Create a new array from object with same keys - javascript

I have a array of objects. I want to merge the objects into a single array and use one of the value as key. In the example below I have a data array which I'm getting from the server as a response to a API and I want to use call_id as a key to index the response into a new array.
I've tried:
data.map(function(index, elem) {responses[index.call_id] = index;})
but this obviously only gets the last array and adding a [] gives me an error
Current Array:
[
{
"id": 2,
"survey_id": 1,
"question_id": 2,
"response": 1,
"order_id": null,
"customer_id": 1,
"call_id": 108,
"created_at": "2020-02-20 18:18:47",
"updated_at": "2020-02-20 18:18:47",
"question": "Do you want it gift wrapped?",
"first_name": "Zain",
"sid": "CA1564cda12b7e1364dc967538c7bdf617"
},
{
"id": 1,
"survey_id": 1,
"question_id": 1,
"response": 2,
"order_id": null,
"customer_id": 1,
"call_id": 108,
"created_at": "2020-02-20 18:18:32",
"updated_at": "2020-02-20 18:18:32",
"question": "Is your order confirmed?",
"first_name": "Zain",
"sid": "CA1564cda12b7e1364dc967538c7bdf617"
},
{
"id": 1,
"survey_id": 1,
"question_id": 1,
"response": 2,
"order_id": null,
"customer_id": 1,
"call_id": 109,
"created_at": "2020-02-20 18:18:32",
"updated_at": "2020-02-20 18:18:32",
"question": "Is your order confirmed?",
"first_name": "Zain",
"sid": "CA1564cda12b7e1364dc967538c7bdf617"
}
]
Expected Result
[{
'108': [
{
"id": 2,
"survey_id": 1,
"question_id": 2,
"response": 1,
"order_id": null,
"customer_id": 1,
"call_id": 108,
"created_at": "2020-02-20 18:18:47",
"updated_at": "2020-02-20 18:18:47",
"question": "Do you want it gift wrapped?",
"first_name": "Zain",
"sid": "CA1564cda12b7e1364dc967538c7bdf617"
},
{
"id": 1,
"survey_id": 1,
"question_id": 1,
"response": 2,
"order_id": null,
"customer_id": 1,
"call_id": 108,
"created_at": "2020-02-20 18:18:32",
"updated_at": "2020-02-20 18:18:32",
"question": "Is your order confirmed?",
"first_name": "Zain",
"sid": "CA1564cda12b7e1364dc967538c7bdf617"
}
],
'109' : [
{
"id": 1,
"survey_id": 1,
"question_id": 1,
"response": 2,
"order_id": null,
"customer_id": 1,
"call_id": 109,
"created_at": "2020-02-20 18:18:32",
"updated_at": "2020-02-20 18:18:32",
"question": "Is your order confirmed?",
"first_name": "Zain",
"sid": "CA1564cda12b7e1364dc967538c7bdf617"
}
]
}]

I think that you want something like below, correct?
If yes, let me explain a little bit:
You will use .reduce(), it uses two values as parameter, an accumulator (in this case is an object) and the current value that is being iterated(in this case each object from the array)
Each iteration you check the accumulator to see if the call_id of the current iterated object already exists or not, if exists, so you just push the object into it, if not, create a new object with call_id as key.
Note: I'm using an array with less properties just for better visualization of the code
let arr = [{
"id": 2,
"call_id": 108,
"sid": "CA1564cda12b7e1364dc967538c7bdf617"
},
{
"id": 1,
"call_id": 108,
"sid": "CA1564cda12b7e1364dc967538c7bdf617"
},
{
"id": 3,
"call_id": 109,
"sid": "CA1564cda12b7e1364dc967538c7bdf617"
}
]
let result = arr.reduce(function(accObj, currentObj) {
accObj[currentObj.call_id] = accObj[currentObj.call_id] || [];
accObj[currentObj.call_id].push(currentObj);
return accObj;
}, {}); //{} is the accumulator object
console.log(result);

var arr = [
{
"id": 2,
"survey_id": 1,
"question_id": 2,
"response": 1,
"order_id": null,
"customer_id": 1,
"call_id": 108,
"created_at": "2020-02-20 18:18:47",
"updated_at": "2020-02-20 18:18:47",
"question": "Do you want it gift wrapped?",
"first_name": "Zain",
"sid": "CA1564cda12b7e1364dc967538c7bdf617"
},
{
"id": 1,
"survey_id": 1,
"question_id": 1,
"response": 2,
"order_id": null,
"customer_id": 1,
"call_id": 108,
"created_at": "2020-02-20 18:18:32",
"updated_at": "2020-02-20 18:18:32",
"question": "Is your order confirmed?",
"first_name": "Zain",
"sid": "CA1564cda12b7e1364dc967538c7bdf617"
},
{
"id": 1,
"survey_id": 1,
"question_id": 1,
"response": 2,
"order_id": null,
"customer_id": 1,
"call_id": 109,
"created_at": "2020-02-20 18:18:32",
"updated_at": "2020-02-20 18:18:32",
"question": "Is your order confirmed?",
"first_name": "Zain",
"sid": "CA1564cda12b7e1364dc967538c7bdf617"
}
];
var obj = {}
arr.forEach(item => {
if (!obj[item.call_id]) obj[item.call_id] = []
obj[item.call_id].push(item)
})
console.log(obj);

You can initialize an object, and for every item in the array check if object[item.call_id]. If it doesn't, assign that new key the value of [item] (starting off the array). If it does, simply get the value of object[call_id], push the current item into that array, then reassign object[call_id] to the updated array. Then wrap the whole thing in brackets if you want it to be an array.
By creating the object first your avoiding the use of nested loops or reduce which can be inefficient in terms of time efficiency should the dataset grow.
var arr = [
{
"id": 2,
"survey_id": 1,
"question_id": 2,
"response": 1,
"order_id": null,
"customer_id": 1,
"call_id": 108,
"created_at": "2020-02-20 18:18:47",
"updated_at": "2020-02-20 18:18:47",
"question": "Do you want it gift wrapped?",
"first_name": "Zain",
"sid": "CA1564cda12b7e1364dc967538c7bdf617"
},
{
"id": 1,
"survey_id": 1,
"question_id": 1,
"response": 2,
"order_id": null,
"customer_id": 1,
"call_id": 108,
"created_at": "2020-02-20 18:18:32",
"updated_at": "2020-02-20 18:18:32",
"question": "Is your order confirmed?",
"first_name": "Zain",
"sid": "CA1564cda12b7e1364dc967538c7bdf617"
},
{
"id": 1,
"survey_id": 1,
"question_id": 1,
"response": 2,
"order_id": null,
"customer_id": 1,
"call_id": 109,
"created_at": "2020-02-20 18:18:32",
"updated_at": "2020-02-20 18:18:32",
"question": "Is your order confirmed?",
"first_name": "Zain",
"sid": "CA1564cda12b7e1364dc967538c7bdf617"
},
{
"id": 1,
"survey_id": 1,
"question_id": 1,
"response": 2,
"order_id": null,
"customer_id": 1,
"call_id": 109,
"created_at": "2020-02-20 18:18:32",
"updated_at": "2020-02-20 18:18:32",
"question": "Is your order confirmed?",
"first_name": "Zain",
"sid": "CA1564cda12b7e1364dc967538c7bdf617"
},
{
"id": 1,
"survey_id": 1,
"question_id": 1,
"response": 2,
"order_id": null,
"customer_id": 1,
"call_id": 109,
"created_at": "2020-02-20 18:18:32",
"updated_at": "2020-02-20 18:18:32",
"question": "Is your order confirmed?",
"first_name": "Zain",
"sid": "CA1564cda12b7e1364dc967538c7bdf617"
},
{
"id": 1,
"survey_id": 1,
"question_id": 1,
"response": 2,
"order_id": null,
"customer_id": 1,
"call_id": 109,
"created_at": "2020-02-20 18:18:32",
"updated_at": "2020-02-20 18:18:32",
"question": "Is your order confirmed?",
"first_name": "Zain",
"sid": "CA1564cda12b7e1364dc967538c7bdf617"
},
{
"id": 1,
"survey_id": 1,
"question_id": 1,
"response": 2,
"order_id": null,
"customer_id": 1,
"call_id": 109,
"created_at": "2020-02-20 18:18:32",
"updated_at": "2020-02-20 18:18:32",
"question": "Is your order confirmed?",
"first_name": "Zain",
"sid": "CA1564cda12b7e1364dc967538c7bdf617"
},
{
"id": 1,
"survey_id": 1,
"question_id": 1,
"response": 2,
"order_id": null,
"customer_id": 1,
"call_id": 109,
"created_at": "2020-02-20 18:18:32",
"updated_at": "2020-02-20 18:18:32",
"question": "Is your order confirmed?",
"first_name": "Zain",
"sid": "CA1564cda12b7e1364dc967538c7bdf617"
},
{
"id": 1,
"survey_id": 1,
"question_id": 1,
"response": 2,
"order_id": null,
"customer_id": 1,
"call_id": 109,
"created_at": "2020-02-20 18:18:32",
"updated_at": "2020-02-20 18:18:32",
"question": "Is your order confirmed?",
"first_name": "Zain",
"sid": "CA1564cda12b7e1364dc967538c7bdf617"
}
]
const reorderArr = () =>{
let myMap = {}
arr.forEach(x=>{
if (!myMap[x.call_id]){
myMap[x.call_id] = [x]
} else {
var children = myMap[x.call_id]
children.push(x)
myMap[x.call_id] = children
}
})
console.log([myMap])
}
reorderArr()

Related

js get the index number for key of an object

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)
})

Accessing Array Object property in ES6,

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);

how to merge an array and an object that are related?

I need to merge an array and one object that has a same product id, consider these two as an example:
myArr = [
{
"id": 60,
"productID": 7,
"reservationDate": "2021-07-29T00:11:30.88",
"reservationFinishDateAndHour": "2021-07-28T13:00:30.88",
"reservationStartDateAndHour": "2021-07-28T09:00:30.88",
"userID": 70,
},
{
"id": 61,
"productID": 10,
"reservationDate": "2021-07-29T00:11:30.88",
"reservationFinishDateAndHour": "2021-07-28T13:00:30.88",
"reservationStartDateAndHour": "2021-07-28T09:00:30.88",
"userID": 70,
}
]
myObj = {
"companyID": 1008,
"createdBy": "Unknown",
"createdDate": "2021-07-27T00:00:00",
"finishWorkingHour": "2021-07-27T18:00:00",
"groupOfImage": null,
"id": 7,
"lastModifiedBy": "Ghods",
"lastModifiedDate": "2021-07-27T00:00:00",
"productCategories": null,
"productDetail": "Casual",
"productName": "Casual",
"productPrice": 0,
"startWorkingHour": "2021-07-27T08:00:00",
}
and I need by comparing productID from myArr to id in myObj get this array:
myResult = [
{
"id": 60,
"productID": 7,
"reservationDate": "2021-07-29T00:11:30.88",
"reservationFinishDateAndHour": "2021-07-28T13:00:30.88",
"reservationStartDateAndHour": "2021-07-28T09:00:30.88",
"userID": 70,
},
{
"companyID": 1008,
"createdBy": "Unknown",
"createdDate": "2021-07-27T00:00:00",
"finishWorkingHour": "2021-07-27T18:00:00",
"groupOfImage": null,
"id": 7,
"lastModifiedBy": "Ghods",
"lastModifiedDate": "2021-07-27T00:00:00",
"productCategories": null,
"productDetail": "Casual",
"productName": "Casual",
"productPrice": 0,
"startWorkingHour": "2021-07-27T08:00:00",
}
]
Use Array#filter to get rid of the items with a different productID and push myObj to the resulting array:
var myArr = [
{
"id": 60,
"productID": 7,
"reservationDate": "2021-07-29T00:11:30.88",
"reservationFinishDateAndHour": "2021-07-28T13:00:30.88",
"reservationStartDateAndHour": "2021-07-28T09:00:30.88",
"userID": 70,
},
{
"id": 61,
"productID": 10,
"reservationDate": "2021-07-29T00:11:30.88",
"reservationFinishDateAndHour": "2021-07-28T13:00:30.88",
"reservationStartDateAndHour": "2021-07-28T09:00:30.88",
"userID": 70,
}
]
var myObj = {
"companyID": 1008,
"createdBy": "Unknown",
"createdDate": "2021-07-27T00:00:00",
"finishWorkingHour": "2021-07-27T18:00:00",
"groupOfImage": null,
"id": 7,
"lastModifiedBy": "Ghods",
"lastModifiedDate": "2021-07-27T00:00:00",
"productCategories": null,
"productDetail": "Casual",
"productName": "Casual",
"productPrice": 0,
"startWorkingHour": "2021-07-27T08:00:00",
}
const result = myArr.filter((e, i) => e.productID == myObj.id);
result.push(myObj)
console.log(result);

How to make advanced filter with jQuery

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])
}
}
}
}
}
}
}

Can not read property picture of undefined

I have the following JSON
{
"error": false,
"data": {
"id": 1,
"name": "Jagadesha NH",
"email": "example#gmail.com",
"phone": "9986XXXXXX",
"dob": "1991-06-01",
"gender": "m",
"active": 1,
"created_at": "2017-02-19 21:33:04",
"updated_at": "2017-02-19 21:33:04",
"profile": {
"id": 1,
"user_id": 1,
"picture": "https:\/\/placehold.it\/150x150",
"about": null,
"occupation": null,
"created_at": "2017-02-19 21:33:04",
"updated_at": "2017-02-19 21:33:04"
}
},
"msg": ""
}
every time I try to read picture property I get an error saying
cannot read property picture of undefined
I am reading like
data.profile.picture
i found it like this :
var x={
"error": false,
"data": {
"id": 1,
"name": "Jagadesha NH",
"email": "example#gmail.com",
"phone": "9986xxxxxx",
"dob": "1904-06-01",
"gender": "m",
"active": 1,
"created_at": "2017-02-19 21:33:04",
"updated_at": "2017-02-19 21:33:04",
"profile": {
"id": 1,
"user_id": 1,
"picture": "https:\/\/placehold.it\/150x150",
"about": null,
"occupation": null,
"created_at": "2017-02-19 21:33:04",
"updated_at": "2017-02-19 21:33:04"
}
},
"msg": ""
}
;
alert(x.data.profile.picture);
if you store json in a variable access it with variable and then using dot(<<variable_name>>.data.profile.picture)
you are getting error because data is itself a key or property of object
var a = //Your json
a.data.profile.picture

Categories

Resources