Filter objects from array - javascript

I have an object TS_List with a key thread_ts
and an array tsColValsArray
I want to remove items where tsColValsArray[i] is part of TS_List.thread_ts
This works for the third item in the array
var TS_List1 = TS_List.filter(item => !item.thread_ts.includes(tsColValsArray[2]));
but how do I filter for all the array
I thought it would be something like
var TS_List1 = TS_List.filter(item => !item.thread_ts.includes(tsColValsArray));

You can combile Array#some with Array#filter
var TS_List1 = TS_List.filter(item => !tsColValsArray.some(e => item.thread_ts.includes(e)));

Related

updating value of object inside of array based on id of different object in different array in react

if I have an array like this:
const[arr,setArr] = React.useState([
{label:"dummy01",id:2},
{label:"dummy02",id:5},
])
is there anyway to update arr with such array:
const newArray = [{label:'dummy Altered01',id:2},{label:'different',id:10},{label:'different 02',id:55}}
what I expect to have an array like this :
[
{label:"dummy Altered01",id:2},
{label:"dummy02",id:5},
{label:'different',id:10},
{label:'different 02',id:55}
]
as you can see the object with the id of 2 is updated and other new objects are added to the array without erasing previous dummy02.
I don't know how should I compare id inside of two different arrays
I know this is not the best answer, so here's what I got
let listOfIds = newArray.map(item => item.id)
newArray = newArray.concat(arr.filter(item => !listOfIds.includes(item.id)))
First you map the newArray into a list of IDs.
Then newArray concatenates with the filtered original array (i.e. the array now doesn't have any items with ids in common with the latter )
Finally you update the state by setArr(newArr)
Hope this answers your question
Lets consider
a = [{label:"dummy01",id:2},{label:"dummy02",id:5}]
and new array is
b= [{label:'dummy Altered01',id:2},{label:'different',id:10},{label:'different 02',id:55}]
Now perform itearation over new array
b.forEach((item)=>{
let index = a.findIndex((x)=>x.id == item.id)
if(index > -1){
a[index].label = item.label
}
else
a.push(item)
})
console.log(a)

How to filter items in the correct orderhow

Consider this array of objects and array of items i want to filter the first array to include only the objects related to the names in Array but follows the order of Array and not the order of object
object= [{name:'ali', age:10},{name:'max', age:5},{name:'john', age:6},{name:'well',age:12}]
Array= ['max','well','john']
const filterit= object.filter(item=>{
if(Array.includes(item.name)
return item.name
})
console.log(filterit)
the output result is
[{name:'max', age:5},
{name:'john', age:6},
{name:'well',age:12}]
the filter works perfect and only the objects related to names in Array gets filtered the only problem is that it gets filtered according to their order in the 'object' array and not according to the names order in 'Array' so how to fix this in order to get a filtered array in the same order as in Array cause order is very crucial to me
First of all, this filter will not work as expected, as you return the item name, it will filter the items that have falsy value for name.
const filterit= object.filter(item=>{
if(Array.includes(item.name))
return item.name
})
You shouldn't return item.name, but it should return Array.includes(item.name)
const filterit= object.filter(item => Array.includes(item.name))
And to make the array with the same sorting.
let array = [{name:'ali', age:10},{name:'max', age:5},{name:'john', age:6},{name:'well',age:12}]
let sortedArray = []
let namesArray = ['max','well','john']
namesArray.forEach(name => {
let item = array.find(i => i.name === name)
if (item) sortedArray.push(item)
})
console.log(sortedArray)
Just iterate over the Array itself and filter that obj's array on match
consdt objArr = object= [{name:'ali', age:10},{name:'max', age:5},{name:'john', age:6},{name:'well',age:12}]
const myArr = ['max','well','john']
const result = myArr.filter(elem => {
return objArr.find(item => item.name == elem)
})

Javascript: How to get values of an array at certain index positions

I have an array of values
arr = ["a","b","c","d"]
and I have another array of indexes
indexes = [0,2]
What is the best way to get the values of the array at these indexes ?
If I apply the method to the values above
it should return
["a","c"]
Use Array.map:
arr = ["a","b","c","d"]
indexes = [0,2]
const res = indexes.map(e => arr[e])
console.log(res)

Filter object in javascript

I am trying to filter through an array of objects and delete one of the object but keep the rest inside an array of objects but i keep returning either an array of arrays or i create nested objects. Is it possible to send in an array of objects and return and array of objects without that specific object? Below is the code I have been trying to work with.
function deleteWorkout(workoutName) {
const updatedArray = myWorkoutToDisplay.map((item) => item.newWorkToAdd.filter((workout) => workout.name !== workoutName))
const objectArray = [{updatedArray}]
const newWorkToAdd = objectArray.filter(e => e.length)
const workouts = [{newWorkToAdd}]
setMyWorkoutToDisplay(updatedArray)
}
You can easily do this with Array.prototype.filter. I guess the easiest way to delete 1 object is like this:
//let arr = arrayLike
//let objToDelete = whatever you want to delete
let newArr = arr.filter(obj => obj !== objToDelete)
newArr now has the array, without the deleted item. arr however still has it. To delete item by index, use this:
//let arr = arrayLike
//let ind = index to delete
let newArr = arr.filter((_, index) => index !== ind)

How to generate key value pair objects from an array in typescript

I have an array of ids and wanted to make an array object with adding a key "id" on it in typescript
array = ['6016b86edeccb2444cc78eef','6016b86edeccb2444cc78eee']
result = [{"id":"6016b86edeccb2444cc78eef"},{"id":"6016b878deccb2444cc78ef0"}]
You can use Array#map as follows:
const array = ['6016b86edeccb2444cc78eef','6016b86edeccb2444cc78eee'];
const result = array.map(id => ({id}));
console.log(result);

Categories