0

I have an array of object something like below

Object[0]

canUpload:false
canBeRemoved:true
  type:Object
    allowMultiple:false
    deleted:false
    key:"testValue"

Object[1]
canUpload:true
canBeRemoved:true
 type:Object
   allowMultiple:false
   deleted:false
   key:"testValue2"

I want to remove an elements from array which contains key:testValue

var myValues = this.testData.data3;

        if(!this.testData.canDownload){
  myValues= myValues.filter(function(value){
                    if(!value.canUpload)
                        return value.type.key==='testValue';
                else return false;
                });

But its not removing .Whats the right way to do it? Here is the full code .I can see myValues array of size 2 .If i print myValues after if block its empty. Code pen:http://codepen.io/developer301985/pen/woGBNg

7
  • Is it JavaScript? Commented Nov 12, 2016 at 1:45
  • return value.type.key === 'testValue1' Commented Nov 12, 2016 at 1:45
  • What is expected result of of value.canUpload at if condition? Commented Nov 12, 2016 at 1:59
  • if value.canUpload= false In the array I want to upload.In developer console I dont see execution entering that point if(!value.canUpload) Commented Nov 12, 2016 at 2:04
  • In this case object[0] should be removed Commented Nov 12, 2016 at 2:09

2 Answers 2

1

If your want to filter your array on the basis of two conditions:

  • canUpload attr is false
  • type.key is equal to 'testValue'

so you may want to return false in case of canUpload is true to be as follow:

myValues= myValues.filter(function(value) {
  if(!value.canUpload)
    return value.type.key === 'testValue';
  else return false;
});

Otherwise, you just want to filter on type.key is equal to 'testValue', so it will be as follow:

myValues= myValues.filter(function(value) {
  return value.type.key === 'testValue';
});
Sign up to request clarification or add additional context in comments.

2 Comments

myValues is empty after outer if block. Not sure If I am doing right
Make sure myValues is parsed as array of objects, also if you could get your code on Codepen to clarify your case more, it'll be better.
0

Note, if the callback function you passed into filter returns true, the element will be kept rather than removed. So in your case, you should return false when the value is testValue.

If I understand you correctly, you want to remove item with value.canUpload==false AND type.key === 'testValue'. Then the negate is value.canUpload || value.type.key !== 'testValue'

var myValues = this.testData.data3;

if (!this.testData.canDownload) {
    myValues = myValues.filter(function(value) {
        return value.canUpload || value.type.key !== 'testValue';
    });
}

Let me know whether it works :)

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.