3

I have a array define like :

selectedDocumentID = [];

Next i have function when i get the ID, and other function where i give the id as parameter. When i click in element with id this id is going to this modifyArray. Next i check the id using console.log (its working and i get id). After that i create a new array list that include a selectedDocument array. (it work). And i want to push to list a id if it not exist or if exist just slice it from list...

modifyArray(id) {
  let selectedDocumentID = [];

  let id = 10;

  console.log('this is: ' + id);
  console.log(this.selectedDocumentID);
  let list = this.selectedDocumentID;

  let index = list.findIndex( x => x === id);
  console.log(index);
  if (index !== id) {
    list.push(id);
    console.log(list);
    this.selectedDocumentID = list;
  }
  else {
    list = list.slice(list.indexOf(id));
    console.log(list);
    this.selectedDocumentID = list;
  }
}

So for exmaple. I click for document with id 2,4,5,6.

My selectedDocumentID = [2,4,5,6] and list also have [2,4,5,6].

Next i click to document with id 8 and my arrays = [2,4,5,6,8], after that i click to document with id 5 and my arrays you look like [2,4,6,8]. What i am doing wrong?

Code sample:

2
  • 1
    When you click document with id 5 you get [2,4,6,8]. Isn't that what you want? Commented Dec 29, 2017 at 8:03
  • Yep, this i want. But this code do not do this :/ Commented Dec 29, 2017 at 8:06

4 Answers 4

5

You can use xor lodash function to make your code simpler. For example:

modifyArray(id) {
  this.selectedDocumentID = _.xor(this.selectedDocumentID, [id]);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for this gem, it's insane what you can do if you know how data structures/statistical mathematics works.
2

you can try following code snippet

var index = list.findIndex(x => x==id)
// here you can check specific property for an object whether it exist in your array or not

if (index === -1){
    list.push(id);
}
else console.log("object already exists")

Update The problem in your code is that you are overriding the list

modifyArray(id) {
  let selectedDocumentID = [];

  let id = 10;

  console.log('this is: ' + id);
  console.log(this.selectedDocumentID);
  let list = this.selectedDocumentID;

  let index = list.findIndex( x => x === id);
  console.log(index);
  if (index !== id) {
    list.push(id);
    console.log(list);
    this.selectedDocumentID = list;
  }
  else {
  list.slice(list.indexOf(id));
    console.log(list);
    this.selectedDocumentID = list;
  }
}

5 Comments

Please look now at Code Sample :) I tried this way and when i try delete only '5', i get [5,6,8]. And this dont slice 5, but 2 and 3 from array :/
you can use slice operator list.splice(index,1)
If i write there index,1 i got empty array
splice method return the item removed from array
Also i removed list, and work only at this.selectedDocumentID for not duplicate the same value :P You help me a lot :) And replace slice for a splice. With splice its work as is should :)
1

You have to use splice to remove:

list.splice(index, 1);

slice just extracts a portion, which I guess you can also use, but then like this:

list = list.slice(index, index + 1);

Comments

1

You can achieve this by javascript also

var tag_story = [];
function checkElemet()
{  
  var  id_tag = document.getElementById("ele").value,
    position = tag_story.indexOf(id_tag); 
  if ( ~position ) tag_story.splice(position, 1);
  else tag_story.push(id_tag);
  console.log(tag_story);
}
<input id="ele" type="text" placeholder="Enter Value"/>
<input type="button" onclick="checkElemet()" value="check"/>

Comments

Your Answer

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.