When I click a button, I want to get a new list. And the example below
i have this list :
List users = [
{
"id": 31,
"name": "John",
},
{
"id": 23,
"name": "Sami",
},
{
"id": 34,
"name": "Leon",
}
];
when search for id list like [31,34]
i need return this result
List users = [
{
"id": 31,
"name": "John",
},
{
"id": 34,
"name": "Leon",
}
];
You can filter it out like this
var newList = users.where((e) => e['id']==31 || e['id']==34).toList();
here is an example:
List users = [
{
"id": 31,
"name": "John",
},
{
"id": 23,
"name": "Sami",
},
{
"id": 34,
"name": "Leon",
}
];
List<searchIds> = [31, 23];
List<searchUsers> = [];
for (int i = 0; i < searchIds.length; i++) {
searchUsers.add(users.where((x) => x["id"] == searchIds[i]).toList()[0]);
}
Related
I have an array :
{
"count": 8,
"id": "accountId",
"name": " Account Id",
"values": [
{
"count": 2,
"id": "1234456789000",
"name": "1234456789000"
},
{
"count": 1,
"id": "135792468045",
"name": "135792468045"
},
{
"count": 1,
"id": "309983110022",
"name": "309983110022"
},
{
"count": 2,
"id": "432112426686",
"name": "432112426686"
},
{
"count": 1,
"id": "6ee26149-a156-4665-bd26-a6e46b49a70f",
"name": "6ee26149-a156-4665-bd26-a6e46b49a70f"
},
{
"count": 1,
"id": "927b48ce-efe4-4c20-98f0-ec6c54f59b45",
"name": "927b48ce-efe4-4c20-98f0-ec6c54f59b45"
}
]
}
And I have a 2nd array :
[
{
"count": 2,
"id": "432112426686",
"name": "432112426686"
},
{
"count": 2,
"id": "1234456789000",
"name": "1234456789000"
}
]
The second array, I have filtered based on some requirement.
I want to replace the values from 1st array with second array.
Using typescript code.
Please help here.
If I understand question, you just need iterate by values array and find same id from second array.
function findAndSwitch(object1, array1): void {
object1.values = object1.values.map(el => {
const objToSwitch = array1.find(eleArr => eleArr.id === el.id);
if (objToSwitch) {
return el = objToSwitch;
} else {
return el;
}
});
}
https://stackblitz.com/edit/typescript-dvty4p
Let's say your variable names are obj1 and arr2, respectively. First transform arr2 into an object obj2:
let obj2 = arr2.reduce((acc,cur) => ({...acc,[cur.id]:cur}),{});
Which now looks like this:
{
'432112426686': {
count: 20,
id: '432112426686',
name: '432112426686'
},
'1234456789000': {
count: 20,
id: '1234456789000',
name: '1234456789000'
}
}
Then transform obj1 based on obj2 as follows:
obj1.values = obj1.values.map(value => obj2[value.id] || value);
Please note that since the data in arr2 is identical to the corresponding data in obj1, I have altered the values of count property in arr2 in order to demonstrate that obj1 will be updated accordingly.
DEMO ONE
let obj1 = {
"count": 8,
"id": "accountId",
"name": " Account Id",
"values": [
{
"count": 2,
"id": "1234456789000",
"name": "1234456789000"
},
{
"count": 1,
"id": "135792468045",
"name": "135792468045"
},
{
"count": 1,
"id": "309983110022",
"name": "309983110022"
},
{
"count": 2,
"id": "432112426686",
"name": "432112426686"
},
{
"count": 1,
"id": "6ee26149-a156-4665-bd26-a6e46b49a70f",
"name": "6ee26149-a156-4665-bd26-a6e46b49a70f"
},
{
"count": 1,
"id": "927b48ce-efe4-4c20-98f0-ec6c54f59b45",
"name": "927b48ce-efe4-4c20-98f0-ec6c54f59b45"
}
]
};
let arr2 = [
{
"count": 20,
"id": "432112426686",
"name": "432112426686"
},
{
"count": 20,
"id": "1234456789000",
"name": "1234456789000"
}
];
let obj2 = arr2.reduce((acc,cur) => ({...acc,[cur.id]:cur}),{});
obj1.values = obj1.values.map(value => obj2[value.id] || value);
console.log( obj1 );
--
However, if all you want is to replace all the obj1.values with arr2, all you need is:
obj1.values = [...arr2];
And this will give rise to:
{
count: 8,
id: 'accountId',
name: ' Account Id',
values: [{
count: 20,
id: '432112426686',
name: '432112426686'
},
{
count: 20,
id: '1234456789000',
name: '1234456789000'
}
]
}
DEMO TWO
let obj1 = {
"count": 8,
"id": "accountId",
"name": " Account Id",
"values": [
{
"count": 2,
"id": "1234456789000",
"name": "1234456789000"
},
{
"count": 1,
"id": "135792468045",
"name": "135792468045"
},
{
"count": 1,
"id": "309983110022",
"name": "309983110022"
},
{
"count": 2,
"id": "432112426686",
"name": "432112426686"
},
{
"count": 1,
"id": "6ee26149-a156-4665-bd26-a6e46b49a70f",
"name": "6ee26149-a156-4665-bd26-a6e46b49a70f"
},
{
"count": 1,
"id": "927b48ce-efe4-4c20-98f0-ec6c54f59b45",
"name": "927b48ce-efe4-4c20-98f0-ec6c54f59b45"
}
]
};
let arr2 = [
{
"count": 20,
"id": "432112426686",
"name": "432112426686"
},
{
"count": 20,
"id": "1234456789000",
"name": "1234456789000"
}
];
obj1.values = arr2;
console.log( obj1 );
I have an Array and One String Value. I want to get the Hierarchy of the String into an Array.
For example, I have a string value "Casuals". "Casuals" value is inside the "Shirts" object. "Shirts" value is inside the "Men" object. And "Men" value is inside the "Default Category" object. So, this is how the logic should be work.
Here is my Sample Array:
{
"id": 2,
"name": "Default Category",
"children_data": [
{
"id": 3,
"name": "Men",
"children_data": [
{
"id": 11,
"name": "T-Shirts",
"children_data": [
{
"id": 27,
"name": "Polos"
},
{
"id": 28,
"name": "Tees"
}
]
},
{
"id": 12,
"name": "Shirts",
"children_data": [
{
"id": 30,
"name": "Casuals"
},
{
"id": 31,
"name": "Formals"
}
]
}
]
},
{
"id": 4,
"name": "Women",
"children_data": [
{
"id": 80,
"name": "Western wears",
"children_data": [
{
"id": 81,
"name": "T-Shirts"
},
{
"id": 82,
"name": "Tank & Crop Tops"
}
]
},
{
"id": 21,
"name": "Ethnic wears",
"children_data": [
{
"id": 51,
"name": "Kurta & Kurtis"
},
{
"id": 52,
"name": "Kurta Sets"
}
]
}
]
}
]
}
And I have the value
let myCategory = "Casuals";
So, that I want to get my final value is ["Default Category", "Men", "Shirts", "Casuals"]
I'm still struggling to get the Hierarchy of the value.
Please try below code for your problem. Let me know If you are facing any issue. Please see working demo.
Call getFilterdObject(data,'Polos'), data is your object.
function getFilterdObject(obj,param){
let finalArray =[];
finalArray.push(obj.name);
if(obj['name'] != param && obj['children_data']){
let filterData = obj['children_data'].filter(function search(a) {
var children;
if (a.name === param) {
return true;
}
if (!Array.isArray(a.children_data)) {
return false;
}
children = a.children_data.filter(search);
if (children.length) {
a.children_data = children;
return true;
}
});
if(filterData.length){
getArray(filterData, param);
}
else{
finalArray =[];
}
}
function getArray(obj,param){
if(obj.length){
obj.map((val)=>{
finalArray.push(val.name);
if(val.children_data && val.name != param){
getArray(val.children_data, param);
}
});
}
}
return finalArray;
};
It is necessary to use Depth First Search Algorithm to recursively search a higher object and then use recursive approach to find all parents:
// Depth First Search Algorithm
function getParentNodeByChild(obj, nameToFind) {
if (obj.children_data) {
if (obj.children_data.some(ch => ch.name == nameToFind))
return obj;
else {
for (let item of obj.children_data) {
if (item.children_data) {
let check = this.getParentNodeByChild(item, nameToFind)
if (check) {
return check;
}
}
}
}
}
return null
}
function getParentObject(nameToFind) {
let parentObj;
if (obj.children_data && obj.children_data.some(ch => ch.name == nameToFind))
return obj;
else {
for (let i = 0; i < obj.children_data.length; ++i) {
parentObj = getParentNodeByChild(obj.children_data[i], nameToFind);
if (parentObj)
break;
}
return parentObj;
}
}
const getAllNames = keyName => {
const parentObject = getParentObject(keyName);
if (parentObject != null && parentObject.name != null) {
names.push(parentObject.name)
getAllNames(parentObject.name);
}
}
let names = [];
let keyToFind = 'Casuals';
getAllNames(keyToFind);
names.push(keyToFind);
console.log(`names`, names);
An example:
let obj = {
"id": 2,
"name": "Default Category",
"children_data": [
{
"id": 3,
"name": "Men",
"children_data": [
{
"id": 11,
"name": "T-Shirts",
"children_data": [
{
"id": 27,
"name": "Polos"
},
{
"id": 28,
"name": "Tees"
}
]
},
{
"id": 12,
"name": "Shirts",
"children_data": [
{
"id": 30,
"name": "Casuals"
},
{
"id": 31,
"name": "Formals"
}
]
}
]
},
{
"id": 4,
"name": "Women",
"children_data": [
{
"id": 80,
"name": "Western wears",
"children_data": [
{
"id": 81,
"name": "T-Shirts"
},
{
"id": 82,
"name": "Tank & Crop Tops"
}
]
},
{
"id": 21,
"name": "Ethnic wears",
"children_data": [
{
"id": 51,
"name": "Kurta & Kurtis"
},
{
"id": 52,
"name": "Kurta Sets"
}
]
}
]
}
]
};
// Depth First Search Algorithm
function getParentNodeByChild(obj, nameToFind) {
if (obj.children_data) {
if (obj.children_data.some(ch => ch.name == nameToFind))
return obj;
else {
for (let item of obj.children_data) {
if (item.children_data) {
let check = this.getParentNodeByChild(item, nameToFind)
if (check) {
return check;
}
}
}
}
}
return null
}
function getParentObject(nameToFind) {
let parentObj;
if (obj.children_data && obj.children_data.some(ch => ch.name == nameToFind))
return obj;
else {
for (let i = 0; i < obj.children_data.length; ++i) {
parentObj = getParentNodeByChild(obj.children_data[i], nameToFind);
if (parentObj)
break;
}
return parentObj;
}
}
const getAllNames = keyName => {
const parentObject = getParentObject(keyName);
if (parentObject != null && parentObject.name != null) {
names.push(parentObject.name)
getAllNames(parentObject.name);
}
}
let names = [];
let keyToFind = 'Casuals';
getAllNames(keyToFind);
names.push(keyToFind);
console.log(`names`, names);
I am using Typescript for below problem. I want to search object not simple alphabetic or number in the list.
Below are the two arrays.
I want to get common objects in separate list without using any third party library.
firstArray = [
{
"id": 4,
"name": "Tata"
},
{
"id": 11,
"name": "Maruti"
},
{
"id": 14,
"name": "Mahindra"
}
]
secondArray = [
{
"id": 4,
"name": "Tata"
},
{
"id": 11,
"name": "Maruti"
},
{
"id": 15,
"name": "Hyundai"
},
{
"id": 21,
"name": "Honda"
}
]
// Get Common Elements
// I am getting blank array as output
console.log(firstArray.filter(( make ) => secondArray.includes( make)));
Is there good function or way to find out commons element?
You can use array#filter with array#some. For each object in the first array, check if id and name exist in the other array.
const firstArray = [{ "id": 4, "name": "Tata" }, { "id": 11, "name": "Maruti" }, { "id": 14, "name": "Mahindra" } ],
secondArray = [{ "id": 4, "name": "Tata" }, { "id": 11, "name": "Maruti" }, { "id": 15, "name": "Hyundai" }, { "id": 21, "name": "Honda" } ],
result = firstArray.filter(o => secondArray.some(({id,name}) => o.id === id && o.name === name));
console.log(result);
For ES6, you can also try sets,
For demonstration,
const thirdArray = [...new Set([...firstArray ,...secondArray])];
I have a Grocery List that I am able to create from an array of foods. What I am trying to do is name the array when I store it. I have my copy hung here Plunker
Currently the output looks like
[
{
"id": 3,
"name": "Coconuts"
},
{
"id": 2,
"name": "Peaches"
},
{
"id": 1,
"name": "Oranges"
}
]
I would like it to be something like
[
{"John's List":
{
"id": 3,
"name": "Coconuts"
},
{
"id": 2,
"name": "Peaches"
},
{
"id": 1,
"name": "Oranges"
}}
]
any thoughts or suggestions would be greatly appreciated.
updated your save function like below
$scope.save = function () {
var entity = {};
entity[$scope.name] = $scope.NewList;
$scope.MyList = angular.copy(entity);
};
from
$scope.save = function () {
$scope.MyList = angular.copy($scope.NewList);
};
checkout this update plunker
the JSON output is
{
"john's list": [
{
"id": 3,
"name": "Coconuts",
"Amount": 10,
"Price": 10
},
{
"id": 2,
"name": "Peaches",
"Amount": 5,
"Price": 5
}
]
}
does this matches your expected output??
I'm basically trying to do what is done with this question, but with an array and return all objects using the values of the corresponding array, not just the value:
Filter backbone collection by attribute value
My natural instinct is to use _.filter or _.partition on the persons collection, but am confused on how these are being compared & returned and am not getting the desired outcome.
With the following, passing the id in via the router:
friendsRoute: function(id){
persons = new App.Collections.Persons();
persons.fetch().done(function(){
var single = persons.find(function(i){
return i.get('id') == id;
});
var friendsIds = single.get('friends');
var friends = ?
//var friendsList = new App.Views.FriendsList();
//friendsList.render({ friends: friends });
});
},
I have a friendsIds array (Joe's friends):
friendsIds = [1,4,5]
And trying to get the matched ids from the following JSON and display their names in my friendsList view:
[
{ "id": 1, "name": "Steve", "age": 22, "friends": [4] },
{ "id": 2, "name": "Mary", "age": 18, "friends": [1,3] },
{ "id": 3, "name": "Joe", "age": 43, "friends": [1,4,5] },
{ "id": 4, "name": "Tommy", "age": 19, "friends": [1] },
{ "id": 5, "name": "Leslie", "age": 27, "friends": [2,4] }
]
I think you could use a combination of map and findWhere to do this:
var friends = _.map(friendsIds, function(id) {
return _.findWhere(persons, { 'id': id });
});
var friendsIds = [1,4,5];
var friends = [
{ "id": 1, "name": "Steve", "age": 22, "friends": [4] },
{ "id": 2, "name": "Mary", "age": 18, "friends": [1,3] },
{ "id": 3, "name": "Joe", "age": 43, "friends": [1,4,5] },
{ "id": 4, "name": "Tommy", "age": 19, "friends": [1] },
{ "id": 5, "name": "Leslie", "age": 27, "friends": [2,4] }
];
var filteredFriends = _.filter(friends, function(frnd){
return _.contains(friendsIds, frnd.id);
});
console.log(filteredFriends);
See the difference in performance with filter and map here
Make use of filter and contains
FIDDLE