With an array like the following, how can I filter objects based on the texts.id? For example if I wanted to return this very same array without the texts object reffering to the id 123 how would I go about it? I have access to the id I want to filter out and also the name of the object where its located. I have been trying to figure this out but I am not sure this is doable with the filter method only. Thanks in advance to anyone that helps.
[
0: Object {
name: 'Name 1',
texts[
0: Obj{
id: '123',
body: 'test message'
},
1: Obj{
id: '456',
body: 'test message 2'
}
]
},
1: Object {
name: 'Name 2',
texts[
0: Obj{
id: '789',
body: 'test message3'
},
1: Obj{
id: '101112',
body: 'test message 4'
}
]
}
]
It's not totally clear to me whether you want to remove the entire outer object where the texts array contains an object with a certain id (i.e: in your example, this would remove the whole object with name 'Name 1'), or if you just want to remove the object in the texts array which has a certain id (i.e: leaving the outer object, just removing the object in texts with id 123).
The former has been answered by #lissettdm above, but if it's the latter, you could do something like this:
const filter = "123";
entry.forEach(item => item.texts = item.texts.filter(text => text.id !== filter));
Yes, it is possible using Array.prototype.filter() and Array.prototype.find()
const entry = [{name: "Name 1",texts: [{id: "123",body: "test message"},{id: "456",body: "test message 2"}]},{name: "Name 2",texts: [{id: "789",body: "test message3"},{id: "101112",body: "test message 4"}]}];
const filter= "123";
const output = entry.filter(item => item.texts.find(text=> text.id===filter));
console.log(output);
Related
I have below array and object and I am trying to replace the entire object of the array where id matches.
this.integrationActionsArray = [{id: 1, key: 'some key', value: 'some value'}]
myObject = {id: 1, key: 'another key', value: 'another value'}
so far I have made below attempts to change the one of the entire objects of an array of objects
this.integrationActionsArray
.find(data => {
if (data.id == myId) {
data = myObject
}
})
console.log(this.integrationActionsArray)
Expectation of the above log is something like below
[{id: 1, key: 'another key', value: 'another value'}]
but it is still like
[{id: 1, key: 'some key', value: 'some value'}]
I have tried using forEach, filter, map and every other iterator but still no luck.
When you do data = myObject, what you are expecting does not happen.
data is just a local variable that points to the integrationActionsArray array element in the find callback method. Once you run the above statement, you are not changing anything in the array, instead just reassigning data.
A simple representation of what is happening above.
let data = { a : 1 , b : 2 };
let data1 = data;
data1={x : 1};
//You can not expect data to change
console.log(data);
To fix this grab the index, and then replace the element in the array.
const integrationActionsArray = [{id: 1, key: 'some key', value: 'some value'}];
const myObject = {id: 1, key: 'another key', value: 'another value'};
const myId = 1;
integrationActionsArray
.forEach((data,index) => {
if (data.id == myId) {
integrationActionsArray[index] = myObject
}
})
console.log(integrationActionsArray)
Note: The above code will work with find() but find returns a specific element. And a simple forEach is ideal for the above case.
This question already has answers here:
Merge 2 arrays of objects
(46 answers)
Closed 1 year ago.
Say I have two data arrays for a ticketed event. One is attendees:
[
{name: 'Jack', ticket_code: 'iGh4rT'},
{name: 'Lisa', ticket_code: 'it1ErB'}
]
The other is tickets:
[
{code: 'iGh4rT', name: 'General Admission'},
{code: 'it1ErB', name: 'VIP'}
]
Now say I want to display a table like this:
Name
Ticket Name
Jack
General Admission
Lisa
VIP
I am struggling with doing this efficiently. I can display a table with one array no problem like something like this:
for (let i = 0; i < attendees.length; i++){
const row = `<tr>
<td>${attendees[i].name}</td>
<td>${attendees[i].ticket_code}</td>
</tr>`
document.getElementById('TableBody').innerHTML += row
I need to somehow 'query' the tickets array with the code from the attendees array for that particular person, get the name of the ticket, and supplant the ticket name instead of the code.
With SQL something like this is easy, but is one able to "query" an array and get a specific property? Should I construct a whole new array with the needed info? What is the best way to do this that would work for large unordered datasets?
You could take one of your array as an object with code as key for the object and map the other array with wanted data and the previous stored data from the object.
const
attendees = [{ name: 'Jack', ticket_code: 'iGh4rT' }, { name: 'Lisa', ticket_code: 'it1ErB' }],
tickets = [{ code: 'iGh4rT', name: 'General Admission' }, { code: 'it1ErB', name: 'VIP' }],
ticketsByCode = Object.fromEntries(tickets.map(o => [o.code, o])),
table = attendees.map(({ name, ticket_code }) => [name, ticketsByCode [ticket_code].name]);
console.log(table);
try this:
let a = [
{name: 'Jack', ticket_code: 'iGh4rT'},
{name: 'Lisa', ticket_code: 'it1ErB'}
];
let b = [
{code: 'iGh4rT', name: 'General Admission'},
{code: 'it1ErB', name: 'VIP'}
];
let c = b.map(item => {
return {
tiketName: item.name,
...a.find(itemA => itemA.ticket_code == item.code)
}
});
console.log(c);
I have two arrays
arrayOfItems: [
{
id: '4321-3321-4423',
value: 'some text'
},
{
id: '4322-4654-9875',
value: 'some text again'
}
]
Then the second array
itemX: [
{
id: '3214-6543-4321',
nestedArrayOfItems:[
{id: '4321-3321-4423'}
{id: '3455-8765-7764'}
]
}
]
I need to create a new array based on arrayOfItems that doesn't include any of the id's in the itemX.nestedArrayOfItems
Because of it being a nested Array I'm drawing a blank on what I need to do... I'm searching through Lodash to see if there is something that doesn't involve me using a bunch of for loops.
You can use Array.prototype.filter() and then check if the id exists with
Array.prototype.some() like so:
const arrayOfItems = [
{
id: '4321-3321-4423',
value: 'some text'
},
{
id: '4322-4654-9875',
value: 'some text again'
}
]
const itemX = [
{
id: '3214-6543-4321',
nestedArrayOfItems: [
{id: '4321-3321-4423'},
{id: '3455-8765-7764'}
]
}
]
const res = arrayOfItems.filter(item => !itemX[0].nestedArrayOfItems.some(({ id }) => id === item.id))
console.log(res);
how about this :
let difference = arrayOfItems.filter(x => ! itemX.nestedArrayOfItems.includes(x));
PS : ids must be string
I have an array of objects, for example
arr = [
{
date: "2020-03-20T11:40:07.620Z",
name: "whatever",
id: "abc123"
},
{
date: "2020-03-21T11:21:07.620Z",
name: "whatever1",
id: "def455"
},
{
date: "2020-03-22T11:54:07.620Z",
name: "whatever2",
id: "abc123"
}
]
Actual data is more than this. I've simplified the array.
Here, id is the key which can be same in more than 1 array of objects, for example in 1st and 3rd id is same.
I want to check if more than 1 objects contain the same value (id). If yes, add another array (sameIdArray) in the first object where id is same (1st in this case) and this array will now contain all those objects where that same value (id) was found and remove them from the actual array. The final array structure will be something like this.
arr = [
{
date: "2020-03-20T11:40:07.620Z",
name: "whatever",
id: "abc123",
sameIdArray: [
{
date: "2020-03-22T11:54:07.620Z",
name: "whatever2",
id: "abc123"
}
]
},
{
date: "2020-03-21T11:21:07.620Z",
name: "whatever1",
id: "def455"
}
]
You can use the groupBy functionality. You can group your data by id and use it accordingly.
You can use libraries like underscore or lodash, if using JavaScript
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 5 years ago.
I have a hand-typed database with an object that has categories and a list of words for each category, as below:
var words =
{
sports: [
'baseball', 'football', 'volleyball', 'basketball', 'soccer'],
animals: [
'dog', 'cat', 'elephant', 'crocodile', 'bird'],
entertainment: [
'netflix', 'movies', 'music', 'concert', 'band', 'computer']
}
My HTML has a bootstrap dropdown that will display all categories based on that list. I have the code working to give me the value of the category clicked as a string: as below:
$(document).on('click', '.dropdown-menu li a', function () {
var selectedCategory;
selectedCategory = $(this).text();
//setting value of category to global variable
categorySelected = selectedCategory;
});
I need to be able to find the key in my database from that value.
The problem is that I can't access words."animals"
I need to take the quotation marks off my string to get the list of words like this:
words.animals
How do I do this? I've tried replace() but it doesn't work.
It seems like you're trying to access the list of values corresponding to the category in your words object. Keys can be strings, so words['animals'] would be an example of getting your list of animals.
JavaScript allows variables to be used as keys as well, so you can access it as follows:
words[categorySelected]
You can pass the text(selected value from drop down) to a function to find the key
var words = {
sports: [
'baseball', 'football', 'volleyball', 'basketball', 'soccer'
],
animals: [
'dog', 'cat', 'elephant', 'crocodile', 'bird'
],
entertainment: [
'netflix', 'movies', 'music', 'concert', 'band', 'computer'
]
}
// function to find the key
function findKey(selText) {
//loop through the object
for (var keys in words) {
//get the array
var getArray = words[keys]
//inside each array check if the selected text is present using index of
if (getArray.indexOf(selText) !== -1) {
console.log(keys)
}
}
}
findKey('music')