I am looking to refine my data output by the category names.
How to filter the output items by the cat_array? Whilst preserving my text search filter?
{
"id": 1,
"title": "Title one",
"category_data": {
"2": "Team",
"7": "Queries"
}
},
I had tested using this:
return vm.info.filter(item => this.cat_array.includes(item.category_id))
Codepen example: https://codepen.io/anon/pen/XxNORW?editors=1011
Thanks
One approach - (Didn't take care of the All logic):
this.info.filter(
item => Object.values(item.category_data).some(
cat => this.cat_array.includes(cat))
)
Demo:
var info = [
{
"id": 1,
"title": "Title one",
"category_data": {
"2": "Team",
"7": "Queries"
}
},
{
"id": 2,
"title": "Title two",
"category_data": {
"2": "Team",
"7": "Queries"
}
},
{
"id": 3,
"title": "Title three",
"category_data": {
"2": "Team",
"7": "Queries"
}
},
{
"id": 4,
"title": "Title four",
"category_data": {
"2": "Team",
"7": "Queries"
}
},
{
"id": 5,
"title": "Title five",
"category_data": {
"2": "Team",
"6": "Questions",
"7": "Queries"
}
},
{
"id": 6,
"title": "Title six",
"category_data": {
"2": "Team",
"6": "Questions",
"7": "Queries",
"12": "Fax",
}
}
]
var cat_array = ["Questions"]
console.log(
info.filter(
item => Object.values(item.category_data).some(
cat => cat_array.includes(cat)
)
)
)
Related
I have an array called $customerRecords. I want to group the data in this array by the customer's email.
This is the array below
$customerRecords = [
{
"id": 1,
"note": "This is note 1",
"customer": [
{
"id": 1,
"user_id": 34,
"email": "doe#mailnator.com",
"phone": "9829484857"
}
]
},
{
"id": 2,
"note": "This is note 2",
"customer": [
{
"id": 2,
"user_id": 34,
"email": "john#mailnator.com",
"phone": "9829484857"
}
]
},
{
"id": 3,
"note": "This is a note 3",
"customer": [
{
"id": 2,
"user_id": 34,
"email": "john#mailnator.com",
"phone": "9829484857"
}
]
},
]
This is the expected result I want to achieve so that I can know the group of data that belongs to an email .
{
"doe#mailnator.com": [
{
"id": 1,
"note": "This is note 1",
"customer": [
{
"id": 1,
"user_id": 34,
"email": "doe#mailnator.com",
"phone": "9829484857"
}
]
}
],
"john#mailnator.com": [
{
"id": 2,
"note": "This is note 2",
"customer": [
{
"id": 2,
"user_id": 34,
"email": "john#mailnator.com",
"phone": "9829484857"
}
]
},
{
"id": 3,
"note": "This is a note 3",
"customer": [
{
"id": 2,
"user_id": 34,
"email": "john#mailnator.com",
"phone": "9829484857"
}
]
}
]
}
So this is what I have tried but it's not working:
return collect($customerRecords)->groupBy('customer.email')
you are almost done just define customer 0 item then email
return collect($customerRecords)->groupBy('customer.0.email');
This is how I was able to solve it.
$grouped = [];
foreach($customerRecords as $value) {
foreach($value['customer'] as $cust) {
$grouped[$cust['email']][] = $value;
}
}
I am trying to make a shopping cart API route and I am stuck with this problem because when I am using this statement $carts = \Cart::session($session)->getContent();
I get an object like this:
"cart": {
"1": {
"id": 1,
"name": "Tort cu biscuiți",
"price": 20,
"quantity": "1",
"attributes": {
"image_path": "http://127.0.0.1:8000/storage/images/AkiGKJdxjKNtyoVI034RPL1drLsMntUxLfzqZplV.jpeg"
},
"conditions": []
},
"2": {
"id": 2,
"name": "Tort cu biscuiți",
"price": 20,
"quantity": 2,
"attributes": {
"image_path": "http://127.0.0.1:8000/storage/images/IkAhenLttHWaRD58hNZ460ykWCq7q1sih3vI9H5V.jpeg"
},
"conditions": []
}
}
and I want to convet this to be array of objects. I tryied $cart->toArray(); and didn't work, also I tiyed (array) $cart and I get this:
"cart": {
"\u0000*\u0000items": {
"1": {
"id": 1,
"name": "Tort cu biscuiți",
"price": 20,
"quantity": "1",
"attributes": {
"image_path": "http://127.0.0.1:8000/storage/images/AkiGKJdxjKNtyoVI034RPL1drLsMntUxLfzqZplV.jpeg"
},
"conditions": []
},
"2": {
"id": 2,
"name": "Tort cu biscuiți",
"price": 20,
"quantity": 2,
"attributes": {
"image_path": "http://127.0.0.1:8000/storage/images/IkAhenLttHWaRD58hNZ460ykWCq7q1sih3vI9H5V.jpeg"
},
"conditions": []
}
}
}
may I know what is the problem?
You can do it manuel like this:
$carts = \Cart::session($session)->getContent();
if ($carts) {
$cartsArray = json_decode($carts);
foreach($cartsArray->cart as &$cart){
$cart = (array) $cart;
}
}
dd($cartsArray);
If I have records for users and also records for orders, it is proper to save them like that:
{
"users": [
{"id": "1", "name": "Allan", "age": "40"},
{"id": "2", "name": "Jack", "age": "50"}
],
"orders": [
{ "id": "1", "item": "item 1", "userId": "1"},
{ "id": "2", "item": "item 3", "userId": "1"},
{ "id": "1", "item": "item 4", "userId": "1"},
{ "id": "1", "item": "item 51", "userId": "1"},
{ "id": "2", "item": "item 4", "userId": "1"}
]
}
as you can see I'm coming from the SQL world so I'm saving the 'orders' data with foreign key, is that the right way to save this data as json and if so how can I get all the users with there orders?
Thanks!
If your json object is in the above mentioned structure you can use this code to filter the orders
var oJson = {
"users": [
{"id": "1", "name": "Allan", "age": "40"},
{"id": "2", "name": "Jack", "age": "50"}
],
"orders": [
{ "id": "1", "item": "item 1", "userId": "1"},
{ "id": "2", "item": "item 3", "userId": "1"},
{ "id": "1", "item": "item 4", "userId": "1"},
{ "id": "1", "i`enter code here`tem": "item 51", "userId": "1"},
{ "id": "2", "item": "item 4", "userId": "1"}
]}
for(var i=0; i<oJson.users.length;i++){
for(var j=0; j<oJson.orders.length;j++){
if (oJson.users[i].id == oJson.orders[j].userId) {
console.log( oJson.orders[j].item, oJson.orders[j].userId)
//your logic here
}
}
}
But it is better to use sql joints to merge the orders and return the objects like this
{
"users": [
{"id": "1", "name": "Allan", "age": "40","orders": [{ "id": "1", "item": "item 1", "userId": "1"},{ "id": "2","item": "item 3", "userId": "1"},{ "id": "1", "item": "item 4", "userId": "1"},{ "id": "1", "item": "item 51", "userId": "1"},{ "id": "2", "item": "item 4", "userId": "1"}
]},
{"id": "2", "name": "Jack", "age": "50","orders":[]}
]}
Fetch the objects for json file into two varible
var a=value.users;
var b=value.orders;
value is nothing but the whole json file
then you can match them against each other like
a[0].id==b.[0].id
I got this format in json.
I need unique values of "volume". So in result I'd get only '30' and '40'.
I used loop inside of another loop, but it returns unique values from each product, which is not what I want.
I'm using Angular 1 in Ionic.
{
"data": [
{
"id": 1,
"title": "Category title",
"products": [
{
"id": 1,
"title": "product title",
"volume": "40"
},
{
"id": 2,
"title": "product title",
"volume": "30"
}
]
},
{
"id": 2,
"title": "Another Category title",
"products": [
{
"id": 3,
"title": "product title",
"volume": "40"
},
{
"id": 3,
"title": "product title",
"volume": "30"
}
]
}
]
}
Thanks
Try this it will work :
JSON :
var obj = {
"data": [{
"id": 1,
"title": "Category title",
"products": [{
"id": 1,
"title": "product title",
"volume": "40"
}, {
"id": 2,
"title": "product title",
"volume": "30"
}]
},
{
"id": 2,
"title": "Another Category title",
"products": [{
"id": 3,
"title": "product title",
"volume": "40"
}, {
"id": 3,
"title": "product title",
"volume": "30"
}]
}
]
};
Logic implementation :
var arr = [];
for (var item in obj.data) {
for (var innData in obj.data[item].products) {
var volume = obj.data[item].products[innData].volume;
if(arr.indexOf(volume) == '-1') {
arr.push(volume);
}
}
}
console.log(arr);
Output :
Working fiddle : https://jsfiddle.net/bo2drv5x/
To get the unique values
var uniqueVolumes = $.unique($.map(json.data, function(datum) {
return datum.products.map(function(product) {
return product.volume;
});
});
);
Using core java script you can find like this:
var jsonData = {"data": [
{
"id": 1,
"title": "Category title",
"products": [
{
"id": 1,
"title": "product title",
"volume": "40"
},
{
"id": 2,
"title": "product title",
"volume": "30"
}
]
},
{
"id": 2,
"title": "Another Category title",
"products": [
{
"id": 3,
"title": "product title",
"volume": "40"
},
{
"id": 3,
"title": "product title",
"volume": "30"
}
]
}
]
};
var uniqueArr = [];
var jsonData = jsonData["data"];
var fullObjectLength = Object.keys(jsonData).length
for(var i=0; i<fullObjectLength;i++)
{
//console.log(jsonData[i]["products"]);
var jsonProductsLength = jsonData[i]["products"].length;
for(var j=0; j<jsonProductsLength; j++)
{
var volumeKeyValue = jsonData[i]["products"][j]["volume"];
var itemTobePushedInArray = uniqueArr.indexOf(volumeKeyValue) == -1;
if(itemTobePushedInArray)
{
uniqueArr.push(volumeKeyValue);
console.log(uniqueArr);
}
}
}
I have users indexed with categories as follows
{
id: 1
name: John
categories: [
{
id: 1
name: Category 1
},
{
id: 2
name: Category 2
}
]
},
{
id: 2
name: Mark
categories: [
{
id: 1
name: Category 1
}
]
}
And I'm trying to get all the documents with Category 1 or Category 2 with
{
filter:
{
bool: {
must: [
{
terms: {user.categories.id: [1, 2]}
}
]
}
}
}
But It only returns the first document that has the two categories, what I am doing wrong?
As I understood, terms search that one of the values is contained in the field, so for user 1
user.categories.id: [1, 2]
user 2
user.categories.id: [1]
Categoy id 1 is contained in both documents
The best way to handle this is probably with a nested filter. You'll have to specify the "nested" type in your mapping, though.
I can set up an index like this:
PUT /test_index
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings": {
"doc": {
"properties": {
"categories": {
"type": "nested",
"properties": {
"id": {
"type": "long"
},
"name": {
"type": "string"
}
}
},
"id": {
"type": "long"
},
"name": {
"type": "string"
}
}
}
}
}
then add some docs:
PUT /test_index/doc/1
{
"id": 1,
"name": "John",
"categories": [
{ "id": 1, "name": "Category 1" },
{ "id": 2, "name": "Category 2" }
]
}
PUT /test_index/doc/2
{
"id": 2,
"name": "Mark",
"categories": [
{ "id": 1, "name": "Category 1" }
]
}
PUT /test_index/doc/3
{
"id": 3,
"name": "Bill",
"categories": [
{ "id": 3, "name": "Category 3" },
{ "id": 4, "name": "Category 4" }
]
}
Now I can use a nested terms filter like this:
POST /test_index/doc/_search
{
"query": {
"constant_score": {
"filter": {
"nested": {
"path": "categories",
"filter": {
"terms": {
"categories.id": [1, 2]
}
}
}
},
"boost": 1.2
}
}
}
...
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "test_index",
"_type": "doc",
"_id": "1",
"_score": 1,
"_source": {
"id": 1,
"name": "John",
"categories": [
{
"id": 1,
"name": "Category 1"
},
{
"id": 2,
"name": "Category 2"
}
]
}
},
{
"_index": "test_index",
"_type": "doc",
"_id": "2",
"_score": 1,
"_source": {
"id": 2,
"name": "Mark",
"categories": [
{
"id": 1,
"name": "Category 1"
}
]
}
}
]
}
}
Here is the code I used:
http://sense.qbox.io/gist/668aefe910643b52a3a10d40aca67104491668fc