I have a file config.js which contain various objects and arrays. It is a large file so i decided to keep it as a separate file. I want to read this array called Config.EmojiCategorySpritesheetDimens from my config.js file in a component file called TextDisplay.js. How could this be done using React or React-Redux. Please help
config.js
var Config = {};
Config.Emoji = {
"00a9": ["\u00A9", ["copyright"]],
"00ae": ["\u00AE", ["registered"]],
"203c": ["\u203C", ["bangbang"]],
"2049": ["\u2049", ["interrobang"]],
"2122": ["\u2122", ["tm"]],
...etc
}
Config.EmojiCategorySpritesheetDimens = [
[7, 27],
[4, 29],
[7, 33],
[3, 34],
[7, 34]
];
Using module.export, I succeed reading the table:
var Config = {};
Config.Emoji = {
"00a9": ["\u00A9", ["copyright"]],
"00ae": ["\u00AE", ["registered"]],
"203c": ["\u203C", ["bangbang"]],
"2049": ["\u2049", ["interrobang"]],
"2122": ["\u2122", ["tm"]]
}
Config.EmojiCategorySpritesheetDimens = [
[7, 27],
[4, 29],
[7, 33],
[3, 34],
[7, 34]
];
module.exports = Config;
result:
config.js is then available in all the component defined in TextDisplay.js
Related
I am trying to create a filtering system for some items by checking if the selected items exist in the presented values.
So my selectable array can range from 0 to 6, and each item contains an array of Ints that it is associated with:
let items = [
Item(cat: [1, 2, 3]),
Item(cat: [0, 6]),
Item(cat: []),
Item(cat: [0, 1])
]
I wanted to create a function that would check if any of the cat values were in the selected Ints:
#Published var filteredCats: [Int] = []
func filterByInt(array: [Item]) -> [Item] {
let output = array.filter({
guard let cats = $0.cats else { return true }
for cat in cats {
return filteredCats.contains(cat)
}
})
return output
}
But I'm having issue with the above since it returns in the loop on the first iteration, so if I was searching for 1 in the above items then Item(cat: [0, 1]) exits at false as the first looped check is 0==1.
Essentially I want to be able to do the following (in expanded terms):
let filter = [0, 3, 4]
let items = [
[1, 2, 3],
[2, 3],
[],
[5, 6, 7]
]
items.contains(filter) // --> return the arrays
Sorry if this is basic but I've been trying to find a good solution.
Checking uniqueness is where a Set can help
struct Item {
var cat: Set<Int>
}
let items = [
Item(cat: [1, 2, 3]),
Item(cat: [0, 6]),
Item(cat: []),
Item(cat: [0, 1])
]
let wanted: Set<Int> = [0, 3, 4]
let filtered = items.filter { !$0.cat.isDisjoint(with: wanted) }
I'd suggest someSatisfy (or any in Kotlin):
We can utilize allSatisfy with double negations (one for allSatisfy, the other for contains) for that:
struct Item: CustomDebugStringConvertible {
let cat: [Int]
var debugDescription: String {
"[" + cat.map { String($0) }.joined(separator: ", ") + "]"
}
}
func filterByInt(items: [Item], filter: [Int]) -> [Item] {
// Early return might make sense if the filter is empty.
// guard !filter.isEmpty else { return items }
items.filter { item in
!filter.allSatisfy {
filterItem in !item.cat.contains(filterItem)
}
}
}
let filter = [0, 3, 4]
let items: [Item] = [
.init(cat: [1, 2, 3]),
.init(cat: [2, 3]),
.init(cat: []),
.init(cat: [5, 6, 7])
]
print(filterByInt(items: items, filter: filter))
// [[1, 2, 3], [2, 3]]
print(filterByInt(items: items, filter: [5]))
// [[5, 6, 7]]
print(filterByInt(items: items, filter: []))
// []
I have a 2D array that I'm mapping in jsx
I am trying to access the outer array index inside the second mapping func
Any ideas would be much appreciated
Something like this:
let arr = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
];
{arr.map((innerArr, idx) => {
return innerArr.map((item) => {
return (
<p>
{idx}: {item}
</p>
);
});
})}
I'm having an object like this
{
"GroupA": {
"Parent1": [1, 2, 3],
"Parent2": [1, 2, 3],
"Parent12": [1, 2, 3]
},
"GroupB": {
"Parent13": [1, 2, 3],
"Parent5": [1, 2, 3]
},
"GroupC": {
"Parent7": [1, 2, 3]
}
}
Now i want to filter this object by searching the name of the Parent
For example when I search parent1 the result should be
{
"GroupA": {
"Parent1": [1, 2, 3],
"Parent12": [1, 2, 3]
},
"GroupB": {
"Parent13": [1, 2, 3],
}
}
Here is my solution but it's not working correctly if a Group has many similar Parent name it only return the first one. And when I try to set state it filter like all wrong value
let newData = []
let catalogArr = Object.entries(catalogList)
const handleSearchElement = (e) => {
const value = e.target.value
catalogArr.forEach(catalog => {
let filteredKeys = Object.keys(catalog[1]).filter(name => name.toLowerCase().includes(value.toLowerCase()))
let valuesOfKey
if(filteredKeys[0]) {
valuesOfKey = {
[filteredKeys[0]]: Object.values(catalog[1][filteredKeys[0]])
}
newData.push([catalog[0], {...valuesOfKey}])
}
})
console.log(Object.fromEntries(newData));
setCatalogList(Object.fromEntries(newData))
// console.log(catalogList);
}
You can use Array#reduce to accomplish this pretty easily, however, all of the packing and unpacking of objects using Object.entries and Object.fromEntries to essentially treat them as arrays suggests you may be using the wrong data structure.
If you need to do this repeatedly, look into doing a one-off transformation that arranges the data for O(1) access, for example, by grouping on inner keys rather than outer keys (hard to say since I don't know the data or use case). Or, if you're mostly iterating, consider using arrays.
const data = {
"GroupA": {
"Parent1": [1, 2, 3],
"Parent2": [1, 2, 3],
"Parent12": [1, 2, 3]
},
"GroupB": {
"Parent13": [1, 2, 3],
"Parent5": [1, 2, 3]
},
"GroupC": {
"Parent7": [1, 2, 3]
}
};
const targetKey = "parent1";
const res = Object.entries(data).reduce((a, [k, v]) => {
const filtered = Object.entries(v).filter(([k, ]) =>
k.toLowerCase().includes(targetKey.toLowerCase())
);
if (filtered.length) {
a[k] = Object.fromEntries(filtered);
}
return a;
}, {});
console.log(res);
arr = [
{
:id=>2,
:start=> "3:30",
break: 30,
num_attendees: 14
},
{
id: 3,
start: "3: 40",
break: 40,
num_attendees: 4
},
{
id: 4,
start: "4: 40",
break: 10,
num_attendees: 40
}
]
When I do the following
arr.map do |hash|
[ hash[:id], hash[:start] ]
end
returns
#=> [[2, "3:30"], [3, "3: 40"], [4, "4: 40"]]
Is there an elegant and efficient way of passing an array like return_keys = [:id, :start] and get the same above values rather than hard coding inside the array.map
Would you consider the following elegant and efficient?
arr.map { |h| h.values_at(:id, :start) }
#=> [[2, "3:30"], [3, "3: 40"], [4, "4: 40"]]
or
arr.map { |h| h.values_at(*return_keys) }
#=> [[2, "3:30"], [3, "3: 40"], [4, "4: 40"]]
I find the following really expressive
keys = [:id, :start]
arr.map {|hash| hash.slice(*keys).values}
The slice method returns a hash only with the keys passed as parameters (which are preceded by the * operator to convert an array into keyword arguments and avoid hardcoding). Then, the values method gets just the values out of the hash
I have six items in my array: [1, 2, 3, 4, 5, 6]. I would like to group array items per 3 items like that: [[1, 2, 3], [4, 5, 6]]. Is it possible with underscore?
You can use array#reduce to group your array element.
const arr = [1, 2, 3, 4, 5, 6],
group = 3,
result = arr.reduce((r,v,i) => {
let index = Math.floor(i/group);
(r[index] = r[index] || [])[i%group] = v;
return r;
},[]);
console.log(result);