Is there any way i can filter values which are present inside Object
[
{
id: "1",
name:"animal_image.jpg"
},
{
id: "2",
name:"fish_image.jpg"
},
{
id: "3",
name:"animal_doc.txt"
},{
id: "4",
name:"fish_doc.txt"
},
{
id: "4",
name:"flower_petals.jpg"
},
{
id: "5",
name:"plant_roots.jpg"
},
{
id: "6",
name:"human_image.jpg"
},
]
i want to filter all the name which contain_image.jpg so output look like this
output=
[ "human_image.jpg",
"anima_image.jpg",
"fish_image.jpg"
]
In this code snippet filtredData is an array of objects where the name includes _image.jpg and output is just an array of names containing _image.jpg
const data = [
{
id: "1",
name: "animal_image.jpg"
},
{
id: "2",
name: "fish_image.jpg"
},
{
id: "3",
name: "animal_doc.txt"
}, {
id: "4",
name: "fish_doc.txt"
},
{
id: "4",
name: "flower_petals.jpg"
},
{
id: "5",
name: "plant_roots.jpg"
},
{
id: "6",
name: "human_image.jpg"
},
]
const filtredData = data.filter(el => el.name.includes("_image.jpg"));
console.log(filtredData);
const output = filtredData.map(el => el.name);
console.log(output);
filter & map
const output = arr
.filter(x => x.name.endsWith('_image.jpg'))
.map(x => x.name);
Related
i am trying to find a list of synced users between two arrays (objArray1 & objArray2) and return the data from objArray1 along with 'aid' in Objarray2. I have the below code working, but i am trying to see if can return 'aid' from objArray2 as well in the below format.
Code sample below
// SystemA
const objArray1 = [
{ id: "1", name: "John" },
{ id: "2", name: "Jack" },
{ id: "3", name: "Sam" },
{ id: "4", name: "Bill" },
];
// SystemB
const objArray2 = [
{ id: "1", name: "John", aid:"uuud2905555" },
{ id: "3", name: "Sam" }, aid:"uuud29029303"
{ id: "5", name: "Bob" }, aid:"uuud29435454"
];
const array1IDs = objArray1.map((item) => {
return item.id
})
// Result: array1IDs = ["1", "2", "3", "4"];
const array2IDs = objArray2.map((item) => {
return item.id
})
// Result: array2IDs = ["1", "3", "5"];
// FIND SYNCED USERS
// Compare the id value of each item in objArray1 with each item of objArray2
// Return the ones that match.
const syncedUsers = objArray1.filter((item) => {
const found = objArray2.find((element) => element.id === item.id);
return found;
});
Required JSON Format, please note that all matching items should be returned from objArray1, with the exception of 'aid' from objArray2
{
"records": [
{
"id": {aid}, // from objArray2
"fields": {
"Name":{name}, // from objArray1
"sid": {id} // from objArray1
}
]
}
Presented below is one possible way to achieve the desired objective.
Code Snippet
// method to create result as "JSON"
const createJSON = (arr1, arr2) => (
// use "stringify" to transform JavaScript object into JSON
JSON.stringify({
// set-up the "records" prop
records: arr2
.filter( // filter to keep only those that have matching "id"
({ id }) => arr1.some(ob => ob.id === id)
)
.map( // de-structure & rename props to match desired objective
({ id : sid, name : Name, aid: id }) => ({
id,
fields: {Name, sid}
})
)
})
);
// SystemA
const objArray1 = [
{ id: "1", name: "John" },
{ id: "2", name: "Jack" },
{ id: "3", name: "Sam" },
{ id: "4", name: "Bill" },
];
// SystemB
const objArray2 = [
{ id: "1", name: "John", aid:"uuud2905555" },
{ id: "3", name: "Sam", aid:"uuud29029303" },
{ id: "5", name: "Bob", aid:"uuud29435454" },
];
console.log('result as a JSON: ', createJSON(objArray1, objArray2));
.as-console-wrapper { max-height: 100% !important; top: 0 }
Explanation
Inline comments added to the snippet above.
EDIT
Use name and id from array-1. Used restOfArr1Props to account for all other props of array-1 objects to be included.
const createJSON = (arr1, arr2) => (
JSON.stringify({
records: arr1
.filter(
({ id }) => arr2.some(ob => ob.id === id)
)
.map(
({ id : sid, name : Name, ...restOfArr1Props }) => ({
id: arr2.find(a2 => a2.id === sid)?.aid ?? 'missing aid',
fields: {
Name, sid, ...restOfArr1Props
},
})
)
})
);
// SystemA
const objArray1 = [
{ id: "1", name: "John", prop1: 'value11', prop2: 'value12' },
{ id: "2", name: "Jack", prop1: 'value21', prop2: 'value22' },
{ id: "3", name: "Sam", prop1: 'value31', prop2: 'value32' },
{ id: "4", name: "Bill", prop1: 'value41', prop2: 'value42' },
];
// SystemB
const objArray2 = [
{ id: "1", name: "John", aid:"uuud2905555" },
{ id: "3", name: "Sam", aid:"uuud29029303" },
{ id: "5", name: "Bob", aid:"uuud29435454" },
];
console.log('result as a JSON: ', createJSON(objArray1, objArray2));
.as-console-wrapper { max-height: 100% !important; top: 0 }
const objArray1 = [
{ id: '1', name: 'John', type: 'bully' },
{ id: '2', name: 'Jack', type: 'sporty' },
{ id: '3', name: 'Sam', type: 'kind' },
{ id: '4', name: 'Bill', type: 'poliet' },
];
const objArray2 = [
{ id: '1', name: 'John', aid: 'uuud2905555' },
{ id: '3', name: 'Sam', aid: 'uuud29029303' },
{ id: '5', name: 'Bob', aid: 'uuud29435454' },
];
const syncedUsers = { records: [] };
for (let user1 of objArray1) {
const foundUser = objArray2.find(user2 => user2.id === user1.id);
if (foundUser) {
syncedUsers.records.push({
id: foundUser.aid,
fields: { sid: user1.id, name: user1.name, ...user1 },
});
}
}
console.log(JSON.stringify(syncedUsers));
Is there any way i can filter files with given extension and then further filter them
for eg: I have .txt extension and i want to get all my .txt from an array
file=
[ "animal_bio.txt",
"xray.pdf",
"fish_bio.txt",
"mammal_doc.txt",
"human_bio.txt",
"machine.jpg"
]
filtered output contain all .txt extension and further it should contain all the files which have _bio.txt name in it.
so output look like
futherFile=
[ "human_bio.txt",
"fish_bio.txt",
"animal_bio.txt"
]
You can use String.protytype.endsWith function to compare the strings with your extension
const file =
[ "animal_bio.txt",
"xray.pdf",
"fish_bio.txt",
"mammal_doc.txt",
"human_bio.txt",
"machine.jpg"
]
result = file.filter((fileName) => fileName.endsWith("_bio.txt"));
console.log(result)
You can use the Array.filter method and use the String.endsWith method to filter. An example -
// List of files
file = ["animal_bio.txt",
"xray.pdf",
"fish_bio.txt",
"mammal_doc.txt",
"human_bio.txt",
"machine.jpg"
]
// Filtering by extension
file.filter(x => x.endsWith(".txt"));
Hope it helped :)
You can easily achieve this result using reduce and match
When matching for the doc or bio, You can even restrict more to get the string only if _doc.txt is at end of the string using Regular expression /_bio.txt$/
const arr = [
{
id: "1",
name: "animal_bio.txt",
},
{
id: "2",
name: "xray.pdf",
},
{
id: "3",
name: "animal_doc.txt",
},
{
id: "4",
name: "fish_doc.txt",
},
{
id: "5",
name: "flower_petals.jpg",
},
{
id: "5",
name: "plant_roots.jpg",
},
{
id: "6",
name: "human_image.jpg",
},
{
id: "7",
name: "human_bio.txt",
},
{
id: "8",
name: "mammal_doc.txt",
},
];
const result = arr.reduce((acc, { name }) => {
if (name.match(/\.txt$/)) {
if (name.match(/_bio/)) {
acc[0].push(name);
} else {
acc[1].push(name);
}
}
return acc;
},
[[], []]
);
console.log(result);
Then you can get the element containing doc and bio using array destructuring as
const [bioArr, docArr] = result;
console.log(bioArr);
console.log(docArr);
const arr = [
{
id: "1",
name: "animal_bio.txt",
},
{
id: "2",
name: "xray.pdf",
},
{
id: "3",
name: "animal_doc.txt",
},
{
id: "4",
name: "fish_doc.txt",
},
{
id: "5",
name: "flower_petals.jpg",
},
{
id: "5",
name: "plant_roots.jpg",
},
{
id: "6",
name: "human_image.jpg",
},
{
id: "7",
name: "human_bio.txt",
},
{
id: "8",
name: "mammal_doc.txt",
},
];
const result = arr.reduce(
(acc, { name }) => {
if (name.match(/\.txt$/)) {
if (name.match(/_bio/)) {
acc[0].push(name);
} else {
acc[1].push(name);
}
}
return acc;
},
[[], []]
);
const [bioArr, docArr] = result;
console.log(bioArr);
console.log(docArr);
you can use filter function from ES6 like:
const txtFile = file.filter((item) => (item.split('_'))[1] === 'bio.txt')
I have object who users and group
users : [
{
id: "5",
name: "Mandy"
},
{
id: "6",
name: "Carla"
},
{
id: "7",
name: "Jacob"
},
{
id: "8",
name: "Timi"
}
],
groups : [
{
users: ["5", "6"],
id: "22",
name: "group 1"
},
{
users: ["7"],
id: "33",
name: "group 2"
}
]
}
I have this function who return objects group : 5,6 : { users: ["5", "6"],
id: "22",
name: "group 1" }
const Group = obj.groups.reduce((group,currentItem) => {
if (!group[currentItem.users]) group[currentItem.users] = [];
group[currentItem.users].push(currentItem)
console.log(typeof group[currentItem.users])
return group
},{})
console.log(usersGroup) -> return object
console.log(Group[7],'Group 7') -> return object
console.log(Group['5,6'],'userGroup 6') -> return undefinde
I would like my function to return id group: -> string after Group[user.id] -> id group
example Group[5] return -> 22
exmaple jsFiddle -> https://jsfiddle.net/z9hemu2s/1/
This question already has answers here:
Merge specific properties of objects together with JavaScript
(3 answers)
Closed 3 years ago.
I would like to know how to combine array values if same id in javascript.
I tried below code
let result = this.getData(obj);
function getData(obj) {
return obj.map(e=>({procode: e.prcode, id: e.id});
}
var obj= [
{
id: "1",
prcode: "dessert"
},{
id: "1",
prcode: "snacks"
}, {
id: "2",
prcode: "cafe"
}, {
id: "4",
prcode: "all"
}
]
Expected Output:
result = [
{id: "1", prcode: "dessert,snacks"},
{id: "2", prcode: "cafe"},
{id: "4", prcode: "all"}
]
You can use reduce alongside Object.values():
var obj = [
{ id: "1", prcode: "dessert" },
{ id: "1", prcode: "snacks" },
{ id: "2", prcode: "cafe" },
{ id: "4", prcode: "all" }
]
const out = obj.reduce((a, v) => {
if(a[v.id]) {
a[v.id].prcode = [a[v.id].prcode, v.prcode].join(',')
} else {
a[v.id] = v
}
return a
}, {})
console.log(Object.values(out))
I'm trying to return all the items which broke a relationship in the following array:
[
{ id: "1", option: { bound_id: "2" }},
{ id: "2", option: { bound_id: "12" }},
{ id: "12", option: { bound_id: "2" }}
]
As you can see each item is linked each other using the property bound_id, if a property broke a relation like:
[
{ id: "1", option: { bound_id: null }},
{ id: "2", option: { bound_id: "12" }},
{ id: "12", option: { bound_id: "2" }}
]
the following result is returned:
[
{ id: "2", option: { bound_id: "12" }}
{ id: "12", option: { bound_id: "2" }}
]
I'm using the following code:
const input = [
{ id: "1", option: { bound_id: null }},
{ id: "2", option: { bound_id: "12" }},
{ id: "12", option: { bound_id: "2" }}
];
const output = input.filter(a => a.option.bound_id);
console.log(output);
I want to include is to insert only the relationship of the items which are next, an example is better:
[
{ id: "1", option: { bound_id: "2" }},
{ id: "2", option: { bound_id: "3" }},
{ id: "12", option: { bound_id: "2" }}
]
as you can see the item with id 2 broke the relation with id 12 and point to an item with id 3 that doesn't exist in the collection, in this scenario the output should be:
[
{ id: "1", option: { bound_id: "2" }},
{ id: "2", option: { bound_id: "3" }}
]
how can I do this using filter?
While using .filter, you might add to a Set of the ids, and .filter by whether the bound_id being iterated over is included in the Set yet (if not, add it to the set; if so, have the item fail the .filter test). Also keep the item if the index is 0, since you always want to keep the first record:
const input = [
{ id: "1", option: { bound_id: "2" }},
{ id: "2", option: { bound_id: "3" }},
{ id: "12", option: { bound_id: "2" }}
];
const alreadyHave = new Set();
const filtered = input.filter(({ option }, index) => {
const { bound_id } = option;
if (!alreadyHave.has(bound_id) || index === 0) {
alreadyHave.add(bound_id);
return true;
}
});
console.log(filtered);
If, per comment, you actually want to always remove the first item, then change the condition to && index !== 0:
const input = [
{ id: "1", option: { bound_id: "2" }},
{ id: "2", option: { bound_id: "3" }},
{ id: "12", option: { bound_id: "2" }}
];
const alreadyHave = new Set();
const filtered = input.filter(({ option }, index) => {
const { bound_id } = option;
if (!alreadyHave.has(bound_id) && index !== 0) {
alreadyHave.add(bound_id);
return true;
}
});
console.log(filtered);
Or, per comment, if the logic for the first item should be the same, remove the index condition entirely:
const input = [
{ id: "1", option: { bound_id: "2" }},
{ id: "2", option: { bound_id: "3" }},
{ id: "12", option: { bound_id: "2" }}
];
const alreadyHave = new Set();
const filtered = input.filter(({ option }, index) => {
const { bound_id } = option;
if (!alreadyHave.has(bound_id)) {
alreadyHave.add(bound_id);
return true;
}
});
console.log(filtered);
My proposal is based on cross checking:
const input1 = [
{ id: "1", option: { bound_id: null }}
];
const input2 = [
{ id: "1", option: { bound_id: "2" }},
{ id: "2", option: { bound_id: "3" }},
{ id: "12", option: { bound_id: "2" }}
];
const input3 = [
{ id: "1", option: { bound_id: null }},
{ id: "2", option: { bound_id: "12" }},
{ id: "12", option: { bound_id: "2" }}
];
const input4 = [
{ id: "1", option: { bound_id: "2" }},
{ id: "2", option: { bound_id: "12" }},
{ id: "12", option: { bound_id: "2" }}
];
var f = (arr) => {
if (arr.length == 1)
return arr;
return arr.filter(function (e, i, a) {
return (!!a[i+1] && e.option.bound_id == a[i+1].id) ||
(!!a[i-1] && e.id == a[i-1].option.bound_id);
})
};
console.log(f(input1));
console.log(f(input2));
console.log(f(input3));
console.log(f(input4));