I have an object that looks like this
category_1: [
[
id: 1,
name: 'Product 1',
tags: []
],
[
id: 2,
name: 'Product 2',
tags: []
]
],
category_2: [
[
id: 3,
name: 'Product 3',
tags: []
],
[
id: 4,
name: 'Product 4',
tags: ['blue']
],
[
id: 5,
name: 'Product 5',
tags: []
],
[
id: 6,
name: 'Product 6',
tags: ['blue']
]
]
and what I'm trying to do is grab everything in my category, for example category_2 and grab only the items that has blue in the tags.
This issue I'm having is that I get the first item and it just loops a couple of times with just that item.
Here is my code
getCategories(){
Object.keys(categories).forEach(category => {
if(category === 'category_2'){
categories[category].forEach(c => {
if(c.tags.includes('blue') === true){
this.categorydata.push(c)
}
});
}
})
}
First of all are you sure you want categories to be arrays of arrays? If you want items of categories to be objects instead of arrays than you are using the wrong notation. If you want your category to be consisted of objects and not arrays, change your code to this:
category_1: [
{ id: 1, name: "Product 1", tags: [] },
{ id: 2, name: "Product 2", tags: [] },
],
category_2: [
{ id: 3, name: "Product 3", tags: [] },
{ id: 4, name: "Product 4", tags: ["blue"] },
{ id: 5, name: "Product 5", tags: [] },
{ id: 6, name: "Product 6", tags: ["blue"] },
],
With that out of the way, you can do what you want using filter function.
Your code should look like this:
getCategories(categoryName, wantedTag){
return categories[categoryName].filter(item => item.tags.includes(wantedTag));
}
Related
I want to render a nested array in a react-native flatlist how can I
achieve that. i also want to be able to view and hide the children of
each item that has the parentId as null an example of the list
[
{
parentId: null,
postId: 'GZ28tWH3SqoU03tyNTJp',
timePosted: 1658089714186,
comment: 'Hello',
id: 'NZhIfTBVNS15X5yONj0h',
children: [
{
parentId: 'NZhIfTBVNS15X5yONj0h',
postId: 'GZ28tWH3SqoU03tyNTJp',
timePosted: 1658089864025,
comment: 'I am fine',
id: 'hNjZumHnbG63g1Yg9p5R',
children: [
{
parentId: 'hNjZumHnbG63g1Yg9p5R',
postId: 'GZ28tWH3SqoU03tyNTJp',
timePosted: 1658177972422,
comment: '# Ebukaa yaaa',
id: '8Boid6WuphQ9SkG3Eegm',
children: [],
},
],
},
{
parentId: 'NZhIfTBVNS15X5yONj0h',
postId: 'GZ28tWH3SqoU03tyNTJp',
timePosted: 1658177624449,
comment: 'Okay',
id: 'ruvwbJA1ezgvEauqPFLa',
children: [
{
parentId: 'ruvwbJA1ezgvEauqPFLa',
postId: 'GZ28tWH3SqoU03tyNTJp',
timePosted: 1658177755359,
comment: '# Ebukaa hello',
id: 'nUrGIqN69x57LoSSjoJQ',
children: [],
},
],
},
],
},
]
you can use sectionlist which is given in react native.
https://reactnative.dev/docs/sectionlist
I'm trying to set the default value given a list of radio items in Sanity Studio.
Code:
export default {
name: 'banner',
title: 'Banner',
type: 'document',
fields: [
{
name: "category",
title: "Category",
description: "Choose a category",
type: "string",
options: {
layout: "radio",
list: [
{ title: "Documentary", value: "documentary" },
{ title: "Fiction", value: "fiction" },
],
},
// set initial value here ?
},
]
}
There is a property called initialValue that can set the default value of a string easily, but I can't figure out how to do this with radio items.
How can I have the radio item Fiction already selected when the page is loaded?
name: 'banner',
title: 'Banner',
type: 'document',
fields: [
{
name: "category",
title: "Category",
description: "Choose a category",
type: "string",
options: {
layout: "radio",
list: [
{ title: "Documentary", value: "documentary" },
{ title: "Fiction", value: "fiction" },
],
},
initialValue: "documentary", // set initialValue's value to one of the `value`s in your list of radio items
},
]
}```
how can i do something like that in React-Native:
data = [
{id:1,title:'Action',games:[
{id:1,title:'Game1'},
{id:2,title:'Game2'},
{id:3,title:'Game3'},
]},
{id:2,title:'Horror',games:[
{id:1,title:'Game1'},
{id:2,title:'Game2'},
{id:3,title:'Game3'},
]},
]
Every time the query string is updated, look for the game within the category.
Returns only the categories that contain a game with the searched characters.
Thank you! :D
I don't know if I understand your question correctly. This is my solution.
If you query for "Game5" you will get whole object that contains query
const data = [
{
id: 1,
title: "Action",
games: [
{ id: 1, title: "Game1" },
{ id: 2, title: "Game2" },
{ id: 3, title: "Game3" },
],
},
{
id: 2,
title: "Horror",
games: [
{ id: 1, title: "Game4" },
{ id: 2, title: "Game5" },
{ id: 3, title: "Game6" },
],
},
];
const query = "Game5";
const result = data.find((category) =>
category.games.find((g) => g.title === query)
);
console.log(result);
You can use 'filter' instead of 'find' and result will be an array.
This is example of filter version. I add one more category so you can see how it filter
const data = [
{
id: 1,
title: "Action",
games: [
{ id: 1, title: "Game1" },
{ id: 2, title: "Game2" },
{ id: 3, title: "Game3" },
],
},
{
id: 2,
title: "Horror",
games: [
{ id: 1, title: "Game1" },
{ id: 2, title: "Game2" },
{ id: 3, title: "Game3" },
],
},
{
id: 3,
title: "Comedy",
games: [
{ id: 1, title: "Game5" },
{ id: 2, title: "Game6" },
{ id: 3, title: "Game7" },
],
},
];
const query = "Game2";
const result = data.filter((category) =>
category.games.find((g) => g.title === query)
);
console.log(result);
And you might want to look at life cycle componentDidUpdate if you write class component, but if you write function component you might want to use useEffect
This is official react hook explaination
EDIT: from your comment you might want something like this
const data = [
{
id: 1,
title: "Action",
games: [
{ id: 1, title: "Game1" },
{ id: 2, title: "Game2" },
{ id: 3, title: "Game3" },
],
},
{
id: 2,
title: "Horror",
games: [
{ id: 1, title: "Game1" },
{ id: 2, title: "Game2" },
{ id: 3, title: "Game3" },
],
},
{
id: 3,
title: "Comedy",
games: [
{ id: 1, title: "Game5" },
{ id: 2, title: "Game6" },
{ id: 3, title: "Game7" },
],
},
];
const query = "Game5";
let result = null;
data.forEach((category) => {
const game = category.games.filter((g) => g.title === query);
if (game.length) result = { ...category, games: game };
});
console.log(result);
Tell a beginner web delepoer how to get all title from all objects in array
this is my array
0: {id: 6, category_id: 2, title: "Test", brand: "Test", serial_number: "2165412315864",…}
1: {id: 7, category_id: 3, title: "Test2", brand: "Test2", serial_number: "2165412315864",…}
2: {id: 8, category_id: 5, title: "New", brand: "New", serial_number: "2165412315864",…}
3: {id: 9, category_id: 1, title: "New2", brand: "New2", serial_number: "2165412315864",…}
Im try to use this code
categories: {
handler(categories) {
console.log('categories: ', categories[title]); //Debug
},
deep: true
}
If you want an array with just the titles you can use Array.map and pick the parts your interested in:
const arr = [{
id: 6,
category_id: 2,
title: "Test",
brand: "Test",
serial_number: "2165412315864"
},
{
id: 7,
category_id: 3,
title: "Test2",
brand: "Test2",
serial_number: "2165412315864"
},
{
id: 8,
category_id: 5,
title: "New",
brand: "New",
serial_number: "2165412315864"
},
{
id: 9,
category_id: 1,
title: "New2",
brand: "New2",
serial_number: "2165412315864"
}
];
const titles = arr.map(({ title }) => title);
console.log(titles);
The categories variable is actually an array here, so you can not use
categories['title']
// Or,
categories.title
here. To get all title property for each category inside the categories array you can use array .map() method like:
categories: {
handler(categories) {
const arr = categories.map(c => c.title)
console.log('titles: ', arr); //Debug
},
deep: true
}
I have Json data that has children elements. I need to bind the store to an editable grid and have the edits populated to the store.
The data tree does get populated into the ItemFileWriteStore. The datagrid displays only the parent data and none of the children data.
SAMPLE.TXT
{
"items": [
{
"profileId": "1",
"profileName": "ABC",
"profileType": "EmailProfile",
"profilePreferences": [
{
"profilePreferenceId": "1",
"displayText": "Bob",
"address": "primary#some.com"
},
{
"profilePreferenceId": "2",
"displayText": "Sally",
"address": "secondary#some.com"
},
{
"profilePreferenceId": "3",
"displayText": "Joe",
"address": "alternate#some.com"
}
]
}
]
}
javascript
var sampleLayout = [
[
{ field: 'profileName', name: 'profileName', width: '100px' },
{ field: 'profilePreferences.displayText', name: 'displayText', width: '100px' },
{ field: 'profilePreferences.address', name: 'address', width: '100px' }
]];
function populateGrid() {
var url = "sample.txt"; //Will be replaced with endpoint URL
dojo.xhrGet({
handleAs: 'json',
url: url,
error: function (e) {
alert("Error: " + e.message);
},
load: showJsonData
});
}
function showJsonData(response, ioArgs) {
var profileStore = new dojo.data.ItemFileWriteStore({
data: {
items: response.items
}
});
var sampleGrid = dijit.byId("sampleGrid");
sampleGrid.store = profileStore;
sampleGrid.startup();
}
you need to be using dojox.grid.TreeGrid or 'fake' the JSON to present every even row with a blank profileName. Two samples follows, one for TreeGrid another on DataGrid - not tested in working environment though.
Given Hierachial JSON:
{
identifier: 'id' // a good custom to make an id pr item, note spaces and odd chars are invalid
items: [{
id: '1',
profileName: 'Admin',
profilePreferences: [
{ id: '1_1', displayText: 'John Doe', address: 'Big Apple' }
{ id: '1_2', displayText: 'Jane Doe', address: 'Hollywood' }
]
}, {
id: '2',
profileName: 'Visitor',
profilePreferences: [
{ id: '2_1', displayText: 'Foo', address: 'Texas' }
{ id: '2_2', displayText: 'Bar', address: 'Indiana' }
]
}]
}
TreeGrid Structure:
{
cells: [
[
{ field: "profileName", name: "profileName", width: "100px" },
{ field: "profilePreferences",
children: [
{ field: "displayText" name: "displayText", width: "100px" },
{ field: "address" name: "address", width: "100px" }
]
]
]
}
reference: dojo docs
Given flattened 'fake-children' JSON:
{
identifier: 'id' // a good custom to make an id pr item, note spaces and odd chars are invalid
items: [{
id: '1',
profileName: 'Admin', preferenceText: '', preferenceAddr: ''
}, {
id: '2',
profileName: '', preferenceText: 'John', preferenceAddr: 'NY'
}, {
id: '3',
profileName: 'Visitor', preferenceText: '', preferenceAddr: ''
}, {
id: '4', // Not with '.' dot seperator like so
profileName: '', preference.Text: 'Jane Doe', preference.Addr: 'Hollywood'
} ]
DataGrid structure:
[[
{'name': 'Profilename', 'field': 'profileName', 'width': '100px'},
{'name': 'User name', 'field': 'preferenceText', 'width': '100px'},
{'name': 'Address', 'field': 'preferenceAddr', 'width': '200px'}
]]
reference dojo docs