1

following adds items to array:

var arrayOptions = [];
function AddToFilterOptionList(mode) {
 arrayOptions.push(mode);
 }

remove item from array:

function RemoveFromFilterOptionList(mode) {
 var index = arrayOptions.indexOf(mode);
 if (index !== -1) {
 arrayOptions.splice(index, 1);
 }}

for example if i call

AddToFilterOptionList('APPLE') - APPLE should be added to array.

If i again call

AddToFilterOptionList('APPLE+FRUIT') - it should remove the the item 'APPLE' from array arrayOptions and should add APPLE+FRUIT

Any time only one word that starts with APPLE can be in array. How to find the word like 'APPLE' in javascript.

I tried with Match() which returns the matching word. IndexOf() returns 1 only if whole word is match but not start of word.

Zee
8,4805 gold badges39 silver badges59 bronze badges
asked May 22, 2015 at 5:03
7
  • You want to know how to filter an array to find those array elements that contain a given string? So searching an array of ['apple', 'grapefruit'] for 'ap' should return 0 and 1, while 'ape' should return only 1? Or is it purely a 'starts-with' search? And I'm assuming you want the array index - or do you want something else? Commented May 22, 2015 at 5:06
  • just care only apple with start of word, Commented May 22, 2015 at 5:07
  • Why won't you split your APPLE+FRUIT string first? Commented May 22, 2015 at 5:07
  • if first APPLE+FRUIT is in array, then when I add APPLE it should remove APPLE+FRUIT. Problem is when I try to remove the item from array. Commented May 22, 2015 at 5:08
  • Should APPLE+FRUIT be removed when FRUIT is added? Commented May 22, 2015 at 5:13

6 Answers 6

1

Cycle through the Array and then use the startsWith method.

void AddToFilterOptionList(String mode) {
 for (i=0; i<arrayOptions.length; i++) {
 if (mode.startsWith(arrayOptions[i] == 1)) {
 array[i] = mode;
 return; // found, so return
 }
 }
 arrayOptions.push(mode); // should only get here if string did not exist.
 }
answered May 22, 2015 at 5:25
Sign up to request clarification or add additional context in comments.

2 Comments

replace if (mode.startsWith(arrayOptions[i] == 1)(this throws error for some reason) with if (mode.startsWith(arrayOptions[i]). Thank you so much.
@user1282609 This solution may not be ideal. Single sized entries can be replaced by longer variations of the same word: example w/ fixed errors. grape and grapefruit are surely not the same thing. It should also be noted that parts of this answer are future spec JS, .startsWith() is barely supported right now, and there are a few syntax errors as well, suggesting that this wasn't even tested.
1

You need to split by + characted and then loop over produced array to add/remove all items:

var arrayOptions = [];
function AddToFilterOptionList(mode) {
 mode.split(/\+/g).forEach(function(el) {
 var index = arrayOptions.indexOf(el);
 if (index !== -1) {
 arrayOptions.splice(index, 1);
 }
 else {
 arrayOptions.push(el);
 }
 });
}
function RemoveFromFilterOptionList(mode) {
 var index = arrayOptions.indexOf(mode);
 if (index !== -1) {
 arrayOptions.splice(index, 1);
 }
}
AddToFilterOptionList('APPLE');
document.write('<p>' + arrayOptions); // expect: APPLE
AddToFilterOptionList('APPLE+FRUIT');
document.write('<p>' + arrayOptions); // expect: FRUIT
AddToFilterOptionList('APPLE+FRUIT+CARROT');
document.write('<p>' + arrayOptions); // expect: APPLE,CARROT

answered May 22, 2015 at 5:47

Comments

1

This will work assuming the 'this+that' pattern is consistent, and that we only care about the starting item.

http://jsbin.com/gefasuqinu/1/edit?js,console

var arr = [];
function remove(item) {
 var f = item.split('+')[0];
 for (var i = 0, e = arr.length; i < e; i++) {
 if (arr[i].split('+')[0] === f) {
 arr.splice(i, 1);
 break;
 }
 }
}
function add(item) {
 remove(item);
 arr.push(item);
}
answered May 22, 2015 at 5:25

Comments

1

UPDATE:

function add (array, fruits) {
 var firstFruit = fruits.split('+')[0]
 var secondFruit = fruits.split('+')[1]
 var found = false
 var output = []
 output = array.map(function (item) {
 if (item.indexOf(firstFruit) > -1) {
 found = true
 return fruits
 }
 else return item
 })
 if (! found) {
 array.push(fruits)
 }
 return output
}
var fruits = []
add(fruits, 'APPLE')
fruits = add(fruits, 'APPLE+GRAPE')
console.log(fruits[0]) // 'APPLE+GRAPE'
fruits = add(fruits, 'APPLE')
console.log(fruits[0]) // 'APPLE'
answered May 22, 2015 at 5:30

1 Comment

Again add APPLE and it is not replacing APPLE+GRAPE.
1

Try this, the code is not optimised though :P

<html>
 <head>
 <script src = "jquery-1.10.2.min.js"></script>
 <script type = "text/javascript">
 var itemList = []; 
 function addItem()
 {
 var item = $('#item').val();
 if(item != '' || item != 'undefined')
 {
 if(itemList.length == 0)
 itemList.push(item);
 else
 {
 for(i=0;i<itemList.length;i++)
 {
 var splittedInputItems = [];
 splittedInputItems = item.split("+");
 var splittedListItems = [];
 splittedListItems = itemList[i].split("+");
 if(splittedListItems[0] == splittedInputItems[0])
 {
 itemList.splice(i,1);
 itemList.push(item);
 return;
 }
 }
 itemList.push(item);
 }
 }
 }
 </script>
 </head>
 <body>
 <input id="item" type = "text"/>
 <input type = "button" value="Add" onclick="addItem()">
 </body>
</html>
answered May 22, 2015 at 6:26

Comments

0
let items = [1, 2, 3, 2, 4, 5, 2, 7];
let item = 2;
for (let i = 0; i < items.length; i++) {
 if (items[i] === item) {
 items.splice(i, 1);
 i = i - 1;
 }
}

If you want to remove the element '2' from items array, it is a way.

answered Jun 21, 2018 at 8:50

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.