0

I want to remove array which is inside of whole array if an object of its array has certain value. I searched the web and I saw removing object or array if it has certain value and I can't find that solves my problem.

I have objects of array which is again wrapped by array. It looks like below:

"products": [
    [
        {
            "product.name": "A",
            "remark.name": "Good"
        },
        {
            "product.name": "B",
            "remark.name": "Good"
        }
    ],
    [
        {
            "product.name": "A",
            "remark.name": "Bad"
        },
        {
            "product.name": "B",
            "remark.name": "Good"
        }
    ]
]

What I want

I want to omit the whole array which contains at least remark.name === Bad So, I should get the final result like below.

"products": [
    [
        {
            "product.name": "A",
            "remark.name": "Good"
        },
        {
            "product.name": "B",
            "remark.name": "Good"
        }
    ]
]

What I've tried

Below code

let result = [];
products.map((product) => {
    var res = _.remove(product, function (n) {
        return n["remark.name"] === "Fail";
    });

    result.push(res);
});

produces following result:

"products": [
    [
        {
            "product.name": "A",
            "remark.name": "Good"
        },
        {
            "product.name": "B",
            "remark.name": "Good"
        }
    ],
    [
        {
            "product.name": "B",
            "remark.name": "Good"
        }
    ]
]

1 Answer 1

2

Use Array#filter for filtering and as filter Array#every where remark.name is different from 'Bad'.

Note: Use single or double quotes for propertyname 'remark.name' because of the dot in it`s name.

let products = [
    [
        {
            "product.name": "A",
            "remark.name": "Good"
        },
        {
            "product.name": "B",
            "remark.name": "Good"
        }
    ],
    [
        {
            "product.name": "A",
            "remark.name": "Bad"
        },
        {
            "product.name": "B",
            "remark.name": "Good"
        }
    ]
];

let filtered = products.filter(arr => arr.every(obj => obj['remark.name'] !== 'Bad'));
console.log(filtered);

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. Perfectly works. Could you please explain why should single quote be used because of dot in property name?
@HtetPhyoNaing obj.remark.name implies obj = { remark: { name: 'Bad' } } while obj['remark.name'] implies obj = { 'remark.name': 'Bad' }
You are right doublequotes works too. The only way what not functions is object.remark.name because this would be the property remark and there the property name.
So if not absolutely necessary avoid propertienames with a dot inside, it could result in missunderstoodness and faults.
Thank you all so very much. Understood.

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.