Display specific flastlist items with specific keys - reactjs

my data :
const DATA = [
{
id: "1",
from: "Canada",
name: "person1",
},
{
id: "2",
from: "Canada",
name: "person2",
},
{
id: "3",
from: "France",
name: "person3",
}];
i need to filter this flatList and only display people from Canada

I think you are looking for something like:
const result = DATA.filter(element=> element.from === "Canada")
Then you can use your result to display it in the format that you want.

Related

How can I group rows in react-data-table-component?

I am facing a problem when I need to group rows in a table using react-data-table-component.
My dataset:
const cars = [{
"brand": "audi",
"model": "a4",
"hp": "200",
},
{
"brand": "audi",
"model": "a5",
"hp": "300",
},
{
"brand": "audi",
"model": "rs6",
"hp": "500",
},
{
"brand": "tesla",
"model": "model 3",
"hp": "200",
},
{
"brand": "tesla",
"model": "model x",
"hp": "450",
}
];
The table should be structured like this:
How can I achieve this?
I am using hook-like style.
One can use cell property to render. So I create some index to track which brand has already been rendered and if a brand is already on the table it just returns an empty cell.
The code I am using:
const [brandIndex, setBrandIndex] = useState<(string | undefined)[]>([]);
const renderBrand = useCallback((row: CarDto) => {
const alreadyRendered = brandIndex.some(b => b === row.brand);
if (alreadyRendered) {
return null;
} else {
setBrandIndex(brandIndex.concat(row.brand));
return <span>{row.brand}</span>;
}
},[setBrandIndex, brandIndex])
//...
{
id: "brand",
name: "Brand",
selector: (row) => row.brand || "",
cell: renderBrand,
sortable: true,
resizable: true,
width: "200px",
},
Unfortunately this code is not working, all brands in my table are empty after rendering. The table renders a lot of times, so I assume that index contains all brands. The last rendering returns null in the first column.

Two kinds of labels for options in select dropdown

Using angularjs, I want to change what is being used as the label. I have objects which are either companies or people, and use a different value to display as the label. So with the example below, my dropdown options should read: Aida Whitburg, Jones Investments, Edison Yuen, using either the name as the value if it's a person or companyName if the entry is a company.
{ id: 1,
name: "Aida Whitburg",
type: "person"
},
{ id: 2,
companyName: "Jones Investments",
type: "company"},
{ id: 3,
name: "Edison Yuen",
type: "person"
}
<select id="entityDropdown"
ng-options="entity as entity.name for entity in ctrl.entities"
ng-model="ctrl.model.markedEntity" class="form-dropdown"
ng-change="ctrl.onChangeModel()">
</select>
make a new object with merged field and use it, something like that:
let people = [
{
"id": 1,
"name": "Aida Whitburg",
"type": "person"
},
{
"id": 2,
"companyName": "Jones Investments",
"type": "company"
},
{
"id": 3,
"name": "Edison Yuen",
"type": "person"
}
].map(p => {
p.label = p.name || p.companyName
return p;
});
console.log(people)

React Native - Sort array based off words

Hey guys i have the following array:
Array [
Object {
"data": "Cat Man",
"id": "1",
},
Object {
"data": "Bat Girl",
"id": "2",
},
Object {
"data": "Mr Penguin",
"id": "3",
},
Object {
"data": "Cheeky Cheetah",
"id": "4",
},
]
I am going to take the users input in the form of a search bar, how can i sort the array based off the users input.
So lets say the user inputs
Bat g
the array would be sorted to:
Array [
Object {
"data": "Bat Girl",
"id": "2",
},
Object {
"data": "Cat Man",
"id": "1",
},
Object {
"data": "Mr Penguin",
"id": "3",
},
Object {
"data": "Cheeky Cheetah",
"id": "4",
},
]
How can I achieve this?
I have been searching around the array sort function:
Array.prototype.sort()
However I have only seen how to sort based off number comparisons I have never seen an array sorted based off string values like a search. Please could someone help me with this!
Here is function to search data using string text.
const searchItem = txt => {
let text = txt.toLowerCase();
let tracks = dataArray;
let filterTracks = tracks.filter(item => {
if (item.data.toLowerCase().match(text)) {
return item;
}
});
console.log('filterTracks', filterTracks);
};
Array Should be like this
var dataArray = [
{
data: 'Cat Man',
id: '1',
},
{
data: 'Bat Girl',
id: '2',
},
{
data: 'Mr Penguin',
id: '3',
},
{
data: 'Cheeky Cheetah',
id: '4',
},
];

NextJs / React: Organizing Array

I'm having issues understanding how to best manipulate an array to get the data I want. From the research I've done, there's multiple ways, but I'm unclear on which is most optimized.
I want to display a simple list, with the items broken down by country, then state, then organized alphabetically by city. The array is formatted as follows:
[
{
id: 1,
name: "Place 1",
state: "Florida",
city: "Boca Raton",
country: "US",
},
{
id: 2,
name: "Place 2",
state: "Florida",
city: "Daytona Beach",
country: "US",
},
{
id: 3,
name: "Place 3",
state: "Kansas",
city: "Lenexa",
country: "US",
},
{
id: 4,
name: "Place 4",
state: "Harju",
city: "Tallinn",
country: "EE",
},
]
An example of the desired outcome is:
US
Florida
Place 1
Place 2
Kansas
Place 3
EE
Harju
Place 4
I see a lot of people saying to utilize ES6 for this, but I'm not sure the best way to approach it. Manipulate the original array response? Is there some way I can loop through them?
Here's an approach that only requires a single loop.
const data = [];
let result = {};
data.forEach(({ name, state, country }) => {
if (!result[country]) {
result[country] = {};
}
if (!result[country][state]) {
result[country][state] = [name];
}
else {
result[country] = {
...result[country],
[state]: [
...result[country][state],
name
]
};
}
});
console.log(result);
Output
{
US: { Florida: [ 'Place 1', 'Place 2' ], Kansas: [ 'Place 3' ] },
EE: { Harju: [ 'Place 4' ] }
}
I'm sure the if-else part can be removed by using spread operator and operator chaining, but I wasn't able to figure that out.
If your environment supports operator chaining, here's a smaller solution
const data = [];
let result = {};
data.forEach(({ name, state, country }) => {
result[country] = {
...result[country],
[state]: [
...(result?.[country]?.[state] || []),
name
]
};
});
console.log(result);

How to update multiple objects in a single collection of mongodb?

I have one collection in mongodb as given below -
[
{
id: "1",
itemName: "pen",
quantity: 10
},
{
id: "2",
itemName: "notebook",
quantity: 20
},
{
id: "3",
itemName: "book",
quantity: 30
}
]
I have to update this collection in one go. From UI I am getting the request array as -
[
{
"id": "1",
"quantity": 12
},
{
"id":"2",
"quantity": 13
}
]
I need to update the corresponding objects. I can run it in a for loop and using
db.collection.update()
I can update it. But is there any way to pass the whole array to update the corresponding objects in one go?

Resources