const data = {
apple: {
type: 'fruit',
image: './fruit/apple.jpg'
},
orange: {
type: 'fruit',
image: './fruit/orange.jpg'
}
carrrot: {
type: 'vegetable',
image: './vegetable/carrot.jpg'
}
}
I am trying to build a search function to loop through an object to return the items I need.
for example, if I want all the fruit, the image of apple and orange will be rendered.
I need something like this:
list = []
for item in data:
if item.type == 'fruit':
list.append(item)
return list
What is the proper code in React Native? Thanks in advance.
Object.keys allow to iterate over properties:
const fruits = Object.keys(data).filter(key => data[key].type === 'fruit')
.map(key => data[key]);
First, we make an array of object properties, then we filter the keys for type fruit, And finally map the key to the object to provide an array of fruits.
//Input
const data = {
apple: {
type: 'fruit',
image: './fruit/apple.jpg'
},
orange: {
type: 'fruit',
image: './fruit/orange.jpg'
},
carrrot: {
type: 'vegetable',
image: './vegetable/carrot.jpg'
}
}
var expectedResult = Object.values(data).filter((item)=> item.type === "fruit")
Related
Let say that I have these two nested objects:
const sourceKeys = {
school: {
name: "Elisa Lemonnier",
students: 250
},
house: {
room : 2,
kids: true,
assets: "elevator",
}
}
const targetKeys = {
school: {
name: "Lucie Faure",
students: 150,
address: "123 Main St.",
phone: "555-555-5555"
},
house: {
room : 4,
kids: false,
assets: "Garden",
shop: "Auchan"
}
}
And I want the targetKeys keep ONLY the keys that are in sourceKeys. So I will get that :
const targetKeysMatchingSourceKeys = {
school: {
name: "Lucie Faure",
students: 150,
},
house: {
room : 4,
kids: false,
assets: "Garden",
}
}
I don't know how to proceed given that is a nested object. So, I will appreciate any help.
thanks you
I have find the solution, here is
const filteredJSON = Object.assign({}, TargetJsonToObject)
// Recursive function to filter the properties of the object
function filterObject(SourceJsonToObject, filteredObj) {
for (const key of Object.keys(filteredObj)) {
// If the key is not present in the source JSON, delete it from filtered JSON
if (!SourceJsonToObject.hasOwnProperty(key)) {
delete filteredObj[key]
} else if (typeof filteredObj[key] === "object") {
// If the key is present in the source JSON and the value is an object, recursively call the function on the nested object
filterObject(SourceJsonToObject[key], filteredObj[key])
}
}
}
filterObject(SourceJsonToObject, TargetJsonToObject)
I would like to sort images according to their type (good, medium, bad) and put them in the corresponding folder.
My data is an object with image name + label
export interface data = { image: string, label: 'good' | 'medium' | 'bad' }
export const DATA: data = {
{ image: 'name-of-image-1, label: 'good' }
{ image: 'name-of-image-2, label: 'bad' },
{ image: 'name-of-image-3, label: 'good' },
...,
{ image: 'name-of-image-n, label: 'medium' }
}
Images are on a server, the path is for instance: smb://test/images/
So I find my first image at smb://test/images/name-of-image-1
I would like to map my DATA array and copy images in the corresponding folder depending of the label. For example, put image with label bad in the folder ./bad.
Have you an idea to do that with Node?
Your provided code example has some issues which are solved here:
export interface Data {
image: string,
label: 'good' | 'medium' | 'bad'
}
export const data: Data[] = [
{ image: 'name-of-image-1', label: 'good' },
{ image: 'name-of-image-2', label: 'bad' },
{ image: 'name-of-image-3', label: 'good' },
{ image: 'name-of-image-n', label: 'medium' }
];
To solve your problem you coude use .reduce to group the image label accordingly:
const sorted = data.reduce((prev, current) => ({
...prev,
[current.label]: [
...prev[current.label],
current.image
]
}), { good: [], medium: [], bad: [] });
The response of sorted is then the following
{
"good": [
"name-of-image-1",
"name-of-image-3"
],
"medium": [
"name-of-image-n"
],
"bad": [
"name-of-image-2"
]
}
I have created an enum type for my object
const MonthType = new GraphQLEnumType({
name: 'monthType',
value: {
JANUARY:{
value: "January"
},
FEBRUARY: {
value: 'February'
},
MARCH: {
value: 'March'
},
MAY: {
value: 'May'
},
JUNE: {
value: 'June'
},
JULY: {
value: 'July'
},
AUGUST: {
value: "August"
},
SEPTEMBER: {
value: 'September'
},
OCTOBER: {
value: 'October'
},
NOVEMEBER: {
value: 'November'
},
DECEMBER: {
value: 'December'
}
}
})
which I am using kind of like this in my Object type
const UserType = new GraphQLObjectType({
name: 'User', // Importance of Name here
fields: () => ({
id: {
type: GraphQLInt
},
userId: {
type: GraphQLInt
},
gradMonth: {
type: MonthType
},
Now, Whenever I start my express server, I am thrown the following error in my code
monthType values must be an object with value names as keys
What I intend to do? I want the user to select, pass the value of the month which should be one of these.
Can someone help me in figuring out why I am getting the following error?
Try replacing value by values at the begining :
const MonthType = new GraphQLEnumType({
name: 'monthType',
values: { // <=== here
//...
I used this as reference but this only shows the first value found but i want all the possible values to be printed.
var inventory = [{ name: 'apples', quantity: 2 }, { name: 'bananas', quantity: 0 }, { name: 'cherries', quantity: 5 }, { name: 'cherries', quantity: 8 }];
function findCherries(fruit) { return fruit.name === 'cherries'; }
console.log(inventory.filter(findCherries));
the filter function is supposed to return you an array, so I'm guessing your problem is simply that the console.log function doesn't display all of it. Try console.log(inventory.filter(findCherries).toString())
I am making a barcode scanner for my school project but i am stuck. I dont know how to scan through this object. I have this object with objects inside, and I need to scan through each object inside storage variable to check its barcode.
var storage = {
bolts: {
barcode: 57263144,
price: 0.5,
name: 'Plain Brackets',
stock: 25,
},
brackets: {
barcode: 13245627,
price: 0.2,
name: '100mm Bolts',
stock: 2,
},
}
I have a variable called barcode, and I need to test this variable if its the same like one of these. I tried using
for (var key in storage){
if (storage[key].barcode === barcode){
}
}
I would like the most simple way to do that.
Use Object.keys:
Object.keys(obj).forEach(function(key) {
console.log(key, obj[key]);
});
Below is the example:
var storage = {
"bolts": {
barcode: 57263144,
price: 0.5,
name: 'Plain Brackets',
stock: 25,
},
"brackets": {
barcode: 13245627,
price: 0.2,
name: '100mm Bolts',
stock: 2,
}
}
var barcode = 57263144;
Object.keys(storage).forEach(function(key) {
if(storage[key].barcode === barcode) { console.log("do something")}
});
A Fiddle:
https://jsfiddle.net/spechackers/34bhthza/
Use the recursive function to verify if exist more nodes in the objects, example:
const complexObj = {
name: "nobody",
address: { number: 22, moreNumbers: [1,2,3,4,5] },
colors: ["green", "red"],
numbersAgain: { first: 1, second: 4 }
};
function scanObj(obj){
for (let i in obj) {
/*
*Do some verificatio, example:
*I'd like to verify all numbers and if the numbers is greater than 3:
*/
if(typeof obj[i] == "number" && obj[i] > 3){ console.log(obj[i]); }
if (typeof obj[i] === "object") {
scanObj(obj[i])
}
}
}
//call the method
scanObj(complexObj);
Output: 22 4 5 4