How to Remove Value from Object Key - javascript

I have an array of objects called active_filters. It contains a key called value which can contain one or more values (which are separated by commas if there are multiples). I'm trying to remove a certain code from the value key using findIndex. I'm not sure what I'm doing wrong...would appreciate any help. Thanks!
remove_code = "SFMR";
active_filters[0] = {id: "data-type", value: "SFAR,CFAR,IFAR,SFMR,FFAR,PDAR,MCMR,EDMR,CDMR,ECMR,EDAR,CDAR,MDMR", type: "filtered-out-by-car-type"};
Code:
var index = active_filters.findIndex(function(e) { return e.value.split(",").indexOf(remove_code) && e.id === _id });
if (index > -1)
active_filters.splice(index, 1);

Iterate the array with [Array.findIndex()][1] to find the element by id. If the element was found, split the value by comma. Use Array.filter() to remove the code, and join back. Assign the result back to the value property.
var remove_code = "SFMR";
var _id = 'data-type';
var active_filters = [{id: "data-type", value: "SFAR,CFAR,IFAR,SFMR,FFAR,PDAR,MCMR,EDMR,CDMR,ECMR,EDAR,CDAR,MDMR", type: "filtered-out-by-car-type"}];
var index = active_filters.findIndex(function(o) {
return o.id === _id;
});
if(index !== -1) {
active_filters[index].value = active_filters[index].value.split(',').filter(function(s) {
return s !== remove_code;
}).join();
}
console.log(active_filters);

You can do the following with filter(), map(), indexOf() and splice():
var remove_code = "SFMR";
var _id = "data-type";
var active_filters = [{id: "data-type", value: "SFAR,CFAR,IFAR,SFMR,FFAR,PDAR,MCMR,EDMR,CDMR,ECMR,EDAR,CDAR,MDMR", type: "filtered-out-by-car-type"}];
var active_filters = active_filters.filter(i => i.id==_id).map(function(e) {
var temp = e.value.split(",");
var index = temp.indexOf(remove_code);
temp.splice(index,1);
e.value = temp.join();
return e;
});
console.log(active_filters);

You could find the index the slice around it and set that to the value. Create a function that takes in the characters that you want to remove then find the index of those characters. After that you can slice from 0 to the specified index, then from the index + 5 (5 to include the comma) to finish out the remainder of the string. Here's an example of that:
const remove_code = "SFMR";
let active_filters = [];
active_filters.push({id: "data-type", value: "SFAR,CFAR,IFAR,SFMR,FFAR,PDAR,MCMR,EDMR,CDMR,ECMR,EDAR,CDAR,MDMR", type: "filtered-out-by-car-type"});
function removeChars(chars){
const index = active_filters[0].value.indexOf(remove_code);
let newString;
newString = active_filters[0].value.slice(0, index);
newString += active_filters[0].value.slice(index+5);
return newString;
}
active_filters[0].value = removeChars(remove_code);
console.log(active_filters[0]);

I'm trying to remove a certain code from the value key using findIndex.
You're splicing the entire array active_filters, what you need to do is split the filter's value and splice it according the remove_code values. To accomplish that, make a reverse loop and for each match execute the function splice.
Finally, join the values again separating them with comma.
var remove_code = "SFMR",
_id = 'data-type',
active_filters = [{id: "data-type", value: "SFAR,CFAR,IFAR,SFMR,FFAR,PDAR,MCMR,EDMR,CDMR,ECMR,EDAR,CDAR,MDMR", type: "filtered-out-by-car-type"}];
active_filters.forEach(function(f) {
if (f.id === _id) {
var values = f.value.split(','),
length = values.length;
while (length--) if (values[length] === remove_code) values.splice(length, 1);
f.value = values.join();
}
});
console.log(active_filters);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://codepen.io/egomezr/pen/dmLLwP.js"></script>

Related

Want to check if one array has a value of another array, and if it does, change the value of the first array and check it again

I am trying to check if one array has a value of another array, and if it does, the value of the other array should be given a number next to it. If there is a value that matches the value in the other array still, the number should be incremented until it no longer matches a value in the other array. I am struggling with this one. Any help is sincerely appreciated!
For example, "Joe" shows in the below code in both arrays. That means "Joe" in the second array should then be "Joe1". Since "Joe1" is also in the array, the "1" in "Joe1" should then be incremented to "Joe2". Any help is very much appreciated!
var arr = [{name:'Bob',num:0},{name:'Joe',num:0},{name:'Alise',num:0}];
var arr2 = [{name:'Frank',num:0},{name:'Joe',num:0},{name:'Jen',num:0},{name:'Joe1',num:0}];
var cnt = 0
arr.forEach(function(element){
arr2.forEach(function(element2){
if(element2.name === element.name){
while(element2.name + element2.num === element.name){
element2['num'] = ++cnt;
}
if(element2.num != 0){
element2.name += element2.num
}
}
})
})
console.log(arr2)
var arr = [{name:'Bob',num:0},{name:'Joe',num:0},{name:'Alise',num:0}];
var arr2 = [{name:'Frank',num:0},{name:'Joe',num:0},{name:'Jen',num:0},{name:'Joe1',num:0}];
var arr3;
arr.forEach(e => {
arr3 = arr2.map(e2 => {
var name = e2.name.match(/[a-zA-Z]*/)[0]
if (e.name === name) {
var number = e2.name.match(/[0-9]+/)
if (number) {
e2.name = name + (+number[0] + 1)
} else {
e2.name = name + 1
}
}
return e2
})
})
console.log(arr3)

Deleting an item from array by comparing array items

Input array:
["temp/1/Lounge/empty",
"temp/1/Lounge/66,66,66,66,66,66,66,66,64,64,64,64…,64,64,64,64,64,64,64", "temp/2/Lounge/empty",
"temp/3/Lounge/empty"]
I have an array of elements as shown above.
Each item has four parts seperated by ('/').
If first three parts are same and fourth part is different for any two items.I want to remove the item having fourth part as 'empty'.
Example:
If fourth part of element has 'empty' in one item and some data like 66,64,…,64,64,64 in another element.
I want to delete the item having 'empty' as fourth part in the array.
I want output as below:
["temp/1/Lounge/66,66,66,66,66,66,66,66,64,64,64,64…,64,64,64,64,64,64,64",
"temp/2/Lounge/empty",
"temp/3/Lounge/empty"]
I tried to split the array items:
for(i=0;i<arr.length;i++){
stringType = message.split('/')[0];
day = message.split('/')[1] ; //day
room = message.split('/')[2] ;
settingData = message.split('/')[3] ;
}
Please help me to compare items and delete from array.
You can do it as follows:
first, store for every array value in a hash map what "4th values" it has;
next, filter the array and explicitly delete array values of which the 4th value is empty and there are other 4th values (we can check this in the created hash map).
function splitValue(value, ignoreCase) {
let split = value.split('/'),
key = split.slice(0, 3).join('/'),
val = split.slice(3).join('/');
if (ignoreCase) {
key = key.toLowerCase();
}
return [key, val];
}
function filter(arr, ignoreCase = false) {
var byKey = {};
for (let value of arr) {
let [key, val] = splitValue(value, ignoreCase);
if (!byKey.hasOwnProperty(key)) {
byKey[key] = [];
}
if (val !== 'empty') {
byKey[key].push(val);
}
}
return arr.filter((value) => {
let [key, val] = splitValue(value, ignoreCase);
return (val !== 'empty' || byKey[key].length === 0);
});
}
console.log(filter([
"temp/1/Lounge/empty",
"temp/1/Lounge/something",
"temp/2/Lounge/empty",
"temp/3/Lounge/empty"]));
console.log(filter([
"temp/1/Lounge/something",
"temp/3/kitchen/something",
"temp/1/Lounge/empty",
"temp/3/Kitchen/empty"]));
console.log(filter([
"temp/1/Lounge/something",
"temp/3/kitchen/something",
"temp/1/Lounge/empty",
"temp/3/Kitchen/empty"], true));
.as-console-wrapper {
max-height: 100% !important;
}
I also demonstrate in the above example how you can ignore casing of letters, so that temp/3/kitchen/... and temp/3/Kitchen/... are treated as belonging to the same group.
You can use this code. It works perfectly what you need
var resultData = ["temp/1/Lounge/empty", "temp/1/Lounge/66,66,66,66,66,66,66,66,64,64,64,64…,64,64,64,64,64,64,64", "temp/2/Lounge/empty","temp/3/Lounge/empty"];
var data = resultData;
for(var i=0; i<data.length; i++){
var iItem = data[i];
var iFirst = iItem.substring(0, iItem.lastIndexOf("/") + 1);
var iLast = iItem.substring(iItem.lastIndexOf("/") + 1, iItem.length);
for(j=i+1 ; j<data.length; j++){
var jItem = data[j];
var jFirst = jItem.substring(0, jItem.lastIndexOf("/") + 1);
var jLast = jItem.substring(jItem.lastIndexOf("/") + 1, jItem.length);
if(iFirst === jFirst && iLast==='empty'){
resultData.splice(i,1);
}
}
}
console.log(resultData);
You can see that data array is exact copy of resultData array to maintain proper loop when you splice the element from them. For further work around and experimenting the long array values, here is the link to working JSFIDDLE

Find the index of a sub array that contains a number

var array = [[2,3,4],[4,5,6],[2,3,9]];
var number = 9;
If I have this nested array and this variable how do I return the index
where the sub-array with the number is. So the final result should be 2 or.
So far I have:
var indexOfRemainingArray = array.filter(function(item,i) {
if(item != number) {
return i;
}
});
I would like to know how to use map or filter functions for this.
Use Array#findIndex to find the index, and use Array#indexOf in the callback to check if the sub array contains the number at least once.
var array = [[2,3,4],[4,5,6],[2,3,9]];
var number = 9;
var indexOfRemainingArray = array.findIndex(function(sub) {
return sub.indexOf(number) !== -1;
});
console.log(indexOfRemainingArray);
And if you need both indexes, you can assign the result of the inner indexOf to a variable:
var array = [[2,3,4],[4,5,9],[2,3,1]];
var number = 9;
var innerIndex;
var indexOfRemainingArray = array.findIndex(function(sub) {
innerIndex = sub.indexOf(number);
return innerIndex !== -1;
});
console.log(indexOfRemainingArray, innerIndex);

Find common objects in multiple arrays by object ID

I've searched SO for a way to do this but most questions only support two arrays (I need a solution for multiple arrays).
I don't want to compare exact objects, I want to compare objects by their ID, as their other parameters may differ.
So here's the example data:
data1 = [{'id':'13','name':'sophie'},{'id':'22','name':'andrew'}, etc.]
data2 = [{'id':'22','name':'mary'},{'id':'85','name':'bill'}, etc.]
data3 = [{'id':'20','name':'steve'},{'id':'22','name':'john'}, etc.]
...
I'd like to return all objects whose ID appears in all arrays, and I don't mind which of the set of matched objects is returned.
So, from the data above, I'd expect to return any one of the following:
{'id':'22','name':'andrew'}
{'id':'22','name':'mary'}
{'id':'22','name':'john'}
Thanks
First, you really need an array of arrays - using a numeric suffix is not extensible:
let data = [ data1, data2, ... ];
Since you've confirmed that the IDs are unique within each sub array, you can simplify the problem by merging the arrays, and then finding out which elements occur n times, where n is the original number of sub arrays:
let flattened = data.reduce((a, b) => a.concat(b), []);
let counts = flattened.reduce(
(map, { id }) => map.set(id, (map.get(id) || 0) + 1), new Map()
);
and then you can pick out those objects that did appear n times, in this simple version they'll all come from the first sub array:
let found = data[0].filter(({ id }) => counts.get(id) === data.length);
Picking an arbitrary (unique) match from each sub array would be somewhat difficult, although picking just one row of data and picking the items from that would be relatively easy. Either would satisfy the constraint from the question.
If you want the unique object by Name
data1 = [{'id':'13','name':'sophie'},{'id':'22','name':'mary'}]
data2 = [{'id':'26','name':'mary'},{'id':'85','name':'bill'}]
data3 = [{'id':'29','name':'sophie'},{'id':'22','name':'john'}]
flattened = [ ...data1, ...data2, ...data3 ];
counts = flattened.reduce(
(map, { name }) => map.set(name, (map.get(name) || 0) + 1), new Map()
);
names = []
found = flattened.filter(({ name }) => {
if ((counts.get(name) > 1) && (!names.includes(name))) {
names.push(name);
return true
}
return false
});
its too many loops but , if u can find the common id which is present in all the arrays then it would make your finding easier i think .you can have one array value as reference to find the common id
var global = [];
for(var i = 0;i<data1.length;i++){
var presence = true;
for(var j=0;j<arrays.length;j++){
var temp = arrays[j].find(function(value){
return data1[i].id == value.id;
});
if(!temp){
presence = false;
break;
}
}
if(presence){
global.push(data1[i].id)
}
}
console.log(global);
var data1 = [{'id':'13','name':'sophie'},{'id':'22','name':'andrew'}];
var data2 = [{'id':'22','name':'mary'},{'id':'85','name':'bill'}];
var data3 = [{'id':'20','name':'steve'},{'id':'22','name':'john'}];
var arrays = [data1, data2, data3];
var global = [];
for(var i = 0;i<data1.length;i++){
var presence = true;
for(var j=0;j<arrays.length;j++){
var temp = arrays[j].find(function(value){
return data1[i].id == value.id;
});
if(!temp){
presence = false;
break;
}
}
if(presence){
global.push(data1[i].id)
}
}
console.log(global);
There's mention you you need n arrays, but also, given that you can:
put all the arrays into an array called data
you can:
combine your arrays
get a list of duplicated IDs (via sort by ID)
make that list unique (unique list of IDs)
find entries in the combined list that match the unique IDs
where the count of those items match the original number of arrays
Sample code:
// Original data
var data1 = [{'id':'13','name':'sophie'},{'id':'22','name':'andrew'}]
var data2 = [{'id':'22','name':'mary'},{'id':'85','name':'bill'}]
var data3 = [{'id':'13','name':'steve'},{'id':'22','name':'john'}]
var arraycount = 3;
// Combine data into a single array
// This might be done by .pushing to an array of arrays and then using .length
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort?v=control
var data = [].concat(data1).concat(data2).concat(data3);
//console.log(data)
// Sort array by ID
// http://stackoverflow.com/questions/840781/easiest-way-to-find-duplicate-values-in-a-javascript-array
var sorted_arr = data.slice().sort(function(a, b) {
return a.id - b.id;
});
//console.log(sorted_arr)
// Find duplicate IDs
var duplicate_arr = [];
for (var i = 0; i < data.length - 1; i++) {
if (sorted_arr[i + 1].id == sorted_arr[i].id) {
duplicate_arr.push(sorted_arr[i].id);
}
}
// Find unique IDs
// http://stackoverflow.com/questions/1960473/unique-values-in-an-array
var unique = duplicate_arr.filter(function(value, index, self) {
return self.indexOf(value) === index;
});
//console.log(unique);
// Get values back from data
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter?v=control
var matches = [];
for (var i = 0; i < unique.length; ++i) {
var id = unique[i];
matches.push(data.filter(function(e) {
return e.id == id;
}))
}
//console.log(matches)
// for data set this will be 13 and 22
// Where they match all the arrays
var result = matches.filter(function(value, index, self) {
return value.length == arraycount;
})
//console.log("Result:")
console.log(result)
Note: There's very likely to be more efficient methods.. I've left this in the hope part of it might help someone
var arr1 = ["558", "s1", "10"];
var arr2 = ["55", "s1", "103"];
var arr3 = ["55", "s1", "104"];
var arr = [arr1, arr2, arr3];
console.log(arr.reduce((p, c) => p.filter(e => c.includes(e))));
// output ["s1"]

Get full value from array using partial value

I have an array like this:
var array = ["xs-1", "sm-10", "md-4"];
Now I want to get the number at the end of a particular value. For example I want to search the array for "md-" and see what number is at the end of that string (should return 4).
I can't do array.indexOf("xs-") because that isn't the whole value. Is there a way to do this?
Using a for loop:
var array = ["xs-1", "sm-10", "md-4"];
var search = "md-";
var found = null;
for (var i = 0; i < array.length; i++) {
if (array[i].indexOf(search) === 0) {
found = array[i];
break; // Note: this is assuming only one match exists - or at least you are
// only interested in the first match
}
}
if (found) {
alert(found);
} else {
alert("Not found");
}
Using .filter:
var array = ["xs-1", "sm-10", "md-4"];
var search = "md-";
var filtered = array.filter(function(item) {
return item.indexOf(search) === 0;
});
// note that here filtered will contain all matched elements, so it might be more than
// one match.
alert(filtered);
Building from #János Weisz's suggestion, you can easily transform your array into an object using .reduce:
var array = ["xs-1", "sm-10", "md-4"];
var search = "md";
var obj = array.reduce(function(prev, item) {
var cells = item.split("-");
prev[cells[0]] = cells[1];
return prev;
}, {});
// note: at this point we have an object that looks like this:
// { xs:1, sm:10, md: 4 }
// if we save this object, we can do lookups much faster than looping
// through an array
// now to find "md", we simply do:
alert(obj[search]);
If you need to do multiple look ups from the same source array, then transforming it into an object may be the most efficient approach overall. You pay the initial price of the transformation, but after than lookups are O(1) versus O(n) for each time you have to search your array. Of course, if you only ever need one item, then probably don't bother.
I recommend using objects for this:
var array = [{'type': 'xs', 'value': 1}, {'type' : 'sm', 'value': '10'}, {'type' : 'md', 'value': '4'}];
This way you can search the array as:
function searchMyArrayByType(array, type) {
var items[];
for (var i = 0; i < array.length; i++)
{
if (array[i].type == type) items.push(array[i].value);
}
return items;
}
var valuesWithMd = searchMyArrayByType(array, 'md');
For more information regarding the structure and use of objects, please refer to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects
You can create a method that takes the prefix you're looking for, the array, and the split character and returns all the numbers in an array:
function findNumberFromPrefix(prefix, arr, splitChar) {
var values = [];
for (var i = 0; i < arr.length; i++) {
if (arr[i].indexOf(prefix) === 0) {
values.push(arr[i].split(splitChar)[1]);
}
}
return values;
}
And call it:
var array = ["xs-1", "sm-10", "md-4"];
var values = findNumberFromPrefix("md-", array, "-");
console.log(values); //["4"]
Demo: http://jsfiddle.net/rn4h9msh/
A more functional approach and assuming you can have have more than one element with the same prefix:
function findPrefix(array, prefix) {
return array.filter(function (a) { return a.indexOf(prefix) === 0; })
.map(function (e) { return e.slice(prefix.length); })
}
If you have only one matching element, do a loop like this:
var array = ["xs-1", "sm-10", "md-4"];
var needle = "md-";
for(i=0;i<array.length;i++) {
if(array[i].indexOf(needle) == 0)
alert(array[i].substr(needle.length, array[i].length));
}
Demo: http://jsfiddle.net/kg0c43ov/
You can do it like this...
var array = ["xs-1", "sm-10", "md-4"];
getValue("md-");
function getValue(search) {
for(var key in array) {
if(array[key].indexOf(search) > -1) {
alert("Array key is: " + key);
alert("Array value is: " + array[key].replace(search, ""));
}
}
}
JSFiddle here.

Categories

Resources