Debug School

rakesh kumar
rakesh kumar

Posted on

How to apply condition on json array of object using every function

my json array of object

var nonNullSocialPrices = extractNonNullSocialPrice(item);
Enter fullscreen mode Exit fullscreen mode
[
    {
        "site": "facebook",
        "price": "24"
    },
    {
        "site": "twitter",
        "price": "67"
    },
    {
        "site": "youtube",
        "price": "90"
    },
    {
        "site": "wordpress",
        "price": "80"
    }
]
Enter fullscreen mode Exit fullscreen mode
if (nonNullSocialPrices.length > 0) {
            // Add a "Select All" checkbox row
            var allSocialSitesAdded = nonNullSocialPrices.every(function (social) {
        var site = social.site.trim();
        console.log(site)
        var cartSocialsObject = JSON.parse(item.cart_socials);
        console.log(cartSocialsObject);
        return itemadminid == isLoggeddata && cartSocialsObject.hasOwnProperty(site);
    });
    console.log(allSocialSitesAdded);
Enter fullscreen mode Exit fullscreen mode

explanation

if (nonNullSocialPrices.length > 0) {
Enter fullscreen mode Exit fullscreen mode

This condition checks if the length of the array nonNullSocialPrices is greater than zero. It ensures that there are elements in the array before proceeding with the subsequent code.

var allSocialSitesAdded = nonNullSocialPrices.every(function (social) {
Enter fullscreen mode Exit fullscreen mode

If the condition above is true, the code proceeds to create a variable allSocialSitesAdded. The variable is assigned the result of the every method applied to the nonNullSocialPrices array.

The every method is used to check if all elements in the array satisfy a certain condition. It takes a callback function as an argument.

 var site = social.site.trim();
    var cartSocialsObject = JSON.parse(item.cart_socials);
    console.log(cartSocialsObject);
Enter fullscreen mode Exit fullscreen mode
{
    "facebook": "24",
    "twitter": "67",
    "youtube": "90",
    "wordpress": "80"
}
Enter fullscreen mode Exit fullscreen mode

Inside the every callback function, these lines extract the site property from each social object in the array. It trims any leading or trailing whitespaces from the site name. It also parses the cart_socials property from the item object as JSON and logs the resulting object to the console.

    return itemadminid == isLoggeddata && cartSocialsObject.hasOwnProperty(site);
});
Enter fullscreen mode Exit fullscreen mode

The return statement within the every callback determines whether every element in nonNullSocialPrices satisfies the given condition. The condition checks if:

itemadminid is equal to isLoggeddata.
The parsed cartSocialsObject has a property named after the site.
The result (allSocialSitesAdded) will be true if all elements in nonNullSocialPrices meet these conditions, and false otherwise.

 console.log(allSocialSitesAdded);
Enter fullscreen mode Exit fullscreen mode

true

    if (!allSocialSitesAdded) {
    html += '<div class="row">';
    html += '<div class="col-md-12">';
    html += '<div  style="background-color:#d1d6f18f" class="alert alert-info" role="alert">';
    html += '<i class="fas fa-info-circle"></i> Please select checkbox to add to cart';
    html += '</div>';
    html += '</div>';
    html += '<div class="col-md-3">' +
        '<input type="checkbox" data-id="' + item.id + '" name="selectAll" class="selectAll">' +
        ' All' +
        '</div>';
    html += '</div>';
}
Enter fullscreen mode Exit fullscreen mode

if allSocialSitesAdded returns true then hide checkbox

Image description

if allSocialSitesAdded returns false then show checkbox

Image description

Top comments (0)