JavaScript - Find and replace word in array - javascript

How would I find a word (in this case a placeholder, e.g _ORGAN_) in an array and replace it with an element's value?
sql = new Array();
$('#system').change(function(){
filter = " topography_index = _ORGAN_";
sql.push(filter);
});
In this case I would want to replace _ORGAN_ with $('#organ_menu').val();

Try this:
// sql array
var sql = ['organ not found', '_ORGAN_ is here'];
var val_to_replace = '_ORGAN_';
var replace_with = 'heart'; // temp value - change it with $('#organ_menu').val()
$.each(sql, function (key, val) {
// search for value and replace it
sql[key] = val.replace(val_to_replace, replace_with);
})
console.log(sql)
JSFiddle: http://jsfiddle.net/d8sZT/

You can simply do by iterating the array and then assign the value to once it find its match.
for (i = 0; i < sql.length; i++) {
if (sql[i] === "_ORGAN_") {
sql[i] = $('#organ_menu').val();
}
}
example fiddle for better understanding.

You can simply iterate over the array and use replace on each element
var organValue = $('#organ_menu').val();
for (var i = 0; i < sql.length; i++) {
sql[i] = sql[i].replace("_ORGAN_", organValue);
}

var regExp = new RegExp(organ, 'g');
$.each(sql, function(index, value) {
sql[index] = value.replace(regExp, 'test');
})

I'd try something like this, using replace:
sql = new Array();
$('#system').change(function(){
filter = " topography_index = _ORGAN_".replace("_ORGAN_", $('#organ_menu').val(), "gi");
sql.push(filter);
});

You can do this:
First find the index of the item:
var index=sql.indexOf("_ORGAN_");
Then insert your new item at that index and remove the first one:
sql.splice(index,1,newitem);
splice

Related

Remove duplicates in array separated by double commas in JavaScript

I have an array in JavaScript like this
var data = [,A_1_VII,VII,V2,,A_1_VII,VII,V2,,A_1_VII,VII,V2,,B_1_XIV,XIV,V3,,B_2_XVI,XVI,V3]
when I alert in JavaScript it gives as below
,A_1_VII,VII,V2
,A_1_VII,VII,V2
,A_1_VII,VII,V2
,B_1_XIV,XIV,V3
,B_2_XVI,XVI,V3
But I want like this which is duplicates removed array
var unique_data = [,A_1_VII,VII,V2,,B_1_XIV,XIV,V3,,B_2_XVI,XVI,V3]
On alert it should give like this
,A_1_VII,VII,V2
,B_1_XIV,XIV,V3
,B_2_XVI,XVI,V3
First Thing your array contains string as a constant that's not going to work.
Secondly, if all of you value are strings you can do it as follows:
var data =[,"A_1_VII","VII","V2",,"A_1_VII","VII","V2",,"A_1_VII","VII","V2",,"B_1_XIV","XIV","V3",,"B_2_XVI","XVI","V3"];
var uniqueArray = data.filter(function(item, pos) {
return data.indexOf(item) == pos;
})
alert(uniqueArray);
Assuming the variables in your array are well defined, you can clean it up and remove duplicates with a for loop:
var data [/* ... */];
var unique_data = [];
for(let i = 0; i < data.length; i++) {
if (data[i] && unique_data.indexOf(data[i]) === -1) {
unique_data.push(data[i]);
}
}
Please note that the code above assumes that your array contains non-object types, otherwise the solution would need to use something more sophisticated than indexOf().
You can create your unique function to remove duplicate entry and empty value from array like this.
var data =[,"A_1_VII,VII","V2,,A_1_VII","VII","V2",,"A_1_VII","VII","V2",,"B_1_XIV,XIV","V3",,"B_2_XVI,XVI,V3"]
var unique_data = uniqueList(data);
alert(unique_data);
function uniqueList(list) {
var uniqueResult = [];
$.each(list, function(i, e) {
if ($.inArray(e, uniqueResult) == -1 &&$.inArray(e, uniqueResult)!="")// chech for unique value and empty value
uniqueResult.push(e);
});
return uniqueResult ;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

javascript match a string from list of url in array and get that value

I have a variable as var input = 'promojam-untitled-promotion-2'; and an array
var prefferedPatterns = [
"https://promojam.live.promojam.dev:5000/promojam-untitled-promotion-2",
"http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-3",
"http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-4",
"http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-5",
"http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-6",
"http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-7",
"http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-8",
"http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-9"
]
I have to find the matched input element from this array. any idea ?
You will have to loop through array and search input in current value using indexOf().
Following is a sample code using .filter()
var input = 'promojam-untitled-promotion-2';
var prefferedPatterns = [
"https://promojam.live.promojam.dev:5000/promojam-untitled-promotion-2",
"http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-3",
"http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-4",
"http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-5",
"http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-6",
"http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-7",
"http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-8",
"http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-9"
]
var output = prefferedPatterns.filter(function(item){
return (item.indexOf(input)>0)
})
console.log(output);
Try this.
var input = 'promojam-untitled-promotion-2';
var prefferedPatterns = [
"https://promojam.live.promojam.dev:5000/promojam-untitled-promotion-2",
"http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-3",
"http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-4",
"http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-5",
"http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-6",
"http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-7",
"http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-8",
"http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-9"
]
for (var i = 0; i < prefferedPatterns.length; i++) {
if (prefferedPatterns[i].indexOf(input) > -1) {
alert(prefferedPatterns[i]);
}
}

Replace one string within array

I have the following array:
etst,tset,tets,ttest,teest,tesst,testt,4est,test,dest
I want to delete the value of an input box from the array, here's what I'm trying:
var el = document.getElementById('searchInput').value; // this is "test"
var toSearchFor = eld.slice(0,10); // the array above
for(var i=0; i < toSearchFor.length; i++) {
toSearchFor[i] = toSearchFor[i].replace(/el/g, "");
}
It's simply not replacing "test" with ""
How can I do that?
You can use Array.filter (see MDN) to filter out the desired value:
var arr = 'etst,tset,tets,ttest,teest,tesst,testt,4est,test,dest'.split(',')
,val = 'test'
document.querySelector('#result')
.innerHTML = arr.filter(function (v) {return v != val});
<div id="result"></div>
A text field example in this jsFiddle
for global replacement of a string stored in a variable u need to create an instance of RegExp explicitly, like this:
var regex = new RegExp(el, "g");
then use it in replace function:
toSearchFor[i] = toSearchFor[i].replace(regex, "");
The problem with your code is in your regular expression: /el/g. This is trying to match the letters el, instead of whatever it's in the el variable. You could have done it using the RegExp construtor.
// ...
regexp = new RegExp(el); // No need to use 'g' here since you're looking for the whole word
toSearchFor[i] = toSearchFor[i].replace(regexp, "");
// ...
Here's another way of doing it:
var eld = ['etst','tset','tets','ttest','teest','tesst','testt','4est','test','dest'];
// var el = document.getElementById('searchInput').value;
var el = 'test';
console.log(eld);
var index = eld.indexOf(el);
if (index >= 0) {
eld[index] = '';
}
console.log(eld);
Here's the output:
["etst", "tset", "tets", "ttest", "teest", "tesst", "testt", "4est", "test", "dest"]
["etst", "tset", "tets", "ttest", "teest", "tesst", "testt", "4est", "", "dest"]
In this case, we're using Array.prototype.indexOf, which returns the first index at which a given element can be found in the array, so that we can access that element directly (if found).
I hope that helps!

Remove text bricks that are defined in an array from input value

I want to edit the value of an input field! To detail i want to delete the text that that is defined an an array from the input:
So if i have for example:
<input value="hello what is your">
and this array:
var arr = ["hello","is"];
I want to change the value of the input to:
<input value="what your">
How should i start? Thanks http://jsfiddle.net/rRXAG/
How should i start?
1) Iteration - Since you already use jQuery, try with $.each.
2) string.indexOf() - returns -1 if it is not present
var arr = ["hello","is"];
$.each(arr, function (i, j) {
var inTxt = $('input').val();
if (inTxt.indexOf(j) != -1) {
$('input').val(inTxt.replace(j, ''));
}
});});
JSFiddle
var val=document.getElementById("yourid").value;
for(var i=0;i<arr.length;i++){
val.replace(arr[i],"")
}
document.getElementById("yourid").value=val;
This regexp do the job :
$('input').first().val().replace(new RegExp(arr.join('|'), 'g'), '');
Fiddle : http://jsfiddle.net/rRXAG/2/
Assuming the values are words separated by spaces, you can do this:
1) Store the values into a map
var map = {};
for (var i in arr) map[arr[i]] = true;
2) Get the value from your input (it's a string) and filter it
var inputVal = $('input').first().val();
var newValue = inputVal.split(" ").filter(function(x) {
return !map[x];
}).join(" ");
// Set new value
$('input').first().val(newValue);

how to check if value is a last csv entry?

i have long csv with data... like 1,2,3,........,120
i want to check if it is last one. but how to do it with javascript?
here iam splitting all csv and getting each one separate.
var movieSRC = CSV;
if (movieSRC.indexOf(',') > -1) {
movieSRC = movieSRC.split(',');
for (var i = 0; i < movieSRC.length; i++) {
***//need to check if it is a last one!!!***
movies.push(movieSRC[i]);
}
//Using split() method
var arr = movieSRC.split(','); //give you an array
var element = arr[arr.length-1]; //get the last element of array
//Ussing substring() and lastIndexOf()
var element = movieSRC.substring(movieSRC.lastIndexOf(',')+1);
As suggested in the comment, you could also use Array.pop() method, however it will remove the element from the array:
var element = movieSRC.split(',').pop();

Categories