1

I'm trying to find and remove an object from an array, or push it to the array based on if it already exists. I've tried a for if loop and forEach loop and can't seem to crack it. Here's what I have so far:

        // object in store to be modified
        this.sorts = [
          { field: "title", direction: "asc" },
          { field: "value", direction: "asc" }, // remove if exists, add if not
          { field: "quality", direction: "asc" },
        ];

        <button @click="handleCheckbox('value', 'asc')">Value</button>; // example

        handleCheckbox(field, dir) {
        this.sorts.forEach((field, i) => {
          if (this.sorts[i].field === field) {
            this.sorts = this.sorts.splice(i, 1); // remove if passed field is found in array
            console.log("deleted=", this.sorts[i]);
            return;
          } else {
            this.sorts.push({ field: field, direction: dir }); // add if passed field is not found
            console.log("pushed=", this.sorts[i]);
            return;
          }
        });
        // state.commit("setSorts", this.sorts);
        }
1
  • 1
    Use .findIndex() to find the entry. If the index is not -1, then splice it out; otherwise push the new entry. Commented May 7, 2020 at 19:57

3 Answers 3

2

You can use findIndex and then push the object into array accordingly.

var arr = [
  { field: "title", direction: "asc" },
  { field: "value", direction: "asc" }, // remove if exists, add if not
  { field: "quality", direction: "asc" },
];

function findObject(obj, value) {
  var index = arr.findIndex(function(item) {
    if (item.field === value) {
      return true;
    }
  });

  if (!index) { 
    arr.push(obj);
  }
}

findObject({ field: "value", direction: "asc" }, 'value');
Sign up to request clarification or add additional context in comments.

Comments

1

You can use findIndex to obtain the index of the object whose field is as the field argument passed to the function and push or splice depending on the index. Here's a possible implementation of the solution using vanillaJs

var sorts = [
          { field: "title", direction: "asc" },
          { field: "value", direction: "asc" },
          { field: "quality", direction: "asc" },
        ];

function handleCheckbox(field, direction) {
  var index = sorts.findIndex(function(sort){
    sort.field == field
    // test checks: console.log(sort.field, field)
  });
    // test checks: console.log(index)
  if (index < 0) { 
    sorts.push({ field, direction});
  }else{
     sorts.splice(index, 1);
    }
}

Comments

1

Try this:

let sorts = [
    { field: "title", direction: "asc" },
    { field: "value", direction: "asc" },
    { field: "quality", direction: "asc" },
];

function handleCheckbox(field, direction) {
    let index = sorts.findIndex(sort => sort.field == field);

    if (index === -1) {
        sorts.push({ field: field, direction: direction });
        return;
    }

    sorts.splice(index, 1);
}

handleCheckbox("value", "asc");
console.log(sorts); // sorts = [ { field: "title", direction: "asc" }, { field: "quality", direction: "asc" } ];

handleCheckbox("new", "asc");
console.log(sorts); // sorts = [ { field: "title", direction: "asc" }, { field: "quality", direction: "asc" }, { field: "new", direction: "asc" } ];

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.