• 0

Javascript array containsObj extension


Question

Hey guys, was writing a test project to refresh myself in a few areas and I came across the need for two extensions in javascript I wrote. They might not be the most efficient methods in the world but if you need them feel free to use them!

/* Returns the first object in the array that matches the designated values (json or single value accepted).
 * If the val parameter is not valid json it can only contain one value. Otherwise it will need to be json
 * that contains a 'matches' element that is an array of values to test against the property.
 * Can be used like so:
 * var arr = new Array();
 * arr.push(new google.maps.LatLng(100.0, 200.9));
 * arr.push(new google.maps.LatLng(300.0, 210.3));
 * var searchElem = arr.containsObj("lat", "{\"matches\":[\"300.0\", \"100.0\"]}");
 * In this example searchElem would return the first object it found so it would return the second element in the array.
*/
if (!Array.prototype.containsObj)
{
    Array.prototype.containsObj = function(prop, val)
    {
        if (this == null)
            return null;

        var t = Object(this);
        if (t.length === 0)
            return null;

        try
        {
            var jsonStr = JSON.parse(val);
            for (var i = 0; i < t.length; i++)
            {
                if (t[i] != null && t[i] != undefined)
                {
                    for (var i2 = 0; i2 < jsonStr["matches"].length; i2++)
                    {
                        var nVal = jsonStr["matches"][i2];
                        //console.log("t[" + i + "][" + prop + "] = " + t[i][prop] + " nVal = " + nVal);
                        if (t[i][prop] == nVal)
                            return t[i];
                    }
                }
            }
        }
        catch(e)
        {

            for (var i = 0; i < t.length; i++)
            {
                if (t[i] != null && t[i] != undefined)
                {
                    if (t[i][prop] == val)
                        return t[i];
                }
            }
        }

        return null;
    }

    /* Returns the first object in the array that matches the designated property/value pairs in the json 'matches' section.
     * So you could call it like so:
     * var arr = new Array();
     * arr.push(new google.maps.LatLng(100.0, 200.9));
     * arr.push(new google.maps.LatLng(300.0, 210.3));
     * var searchElem = arr.containsObjVals("{\"matches\":[{\"propName\":\"lat\", \"propVal\":\"100.0\"}, {\"propName\":\"lng\", \"propVal\":\"200.9\"}]}");
     * assuming the LatLng object contains the properties named 'lat' and 'lng' (without the quotes of course).
     */
    Array.prototype.containsObjVals = function (params)
    {
        //console.log("params: " + params);
        if (this == null)
            return null;

        var t = Object(this);
        if (t.length === 0)
            return null;

        try
        {
            var jsonStr = JSON.parse(params);
            for (var i = 0; i < t.length; i++)
            {
                //console.log("now looping through i: " + i);
                if (t[i] != null && t[i] != undefined)
                {
                    //console.log("t[i] is not null");
                    for (var i2 = 0; i2 < jsonStr["matches"].length; i2++)
                    {
                        var prop = jsonStr["matches"][i2].propName;
                        var val = jsonStr["matches"][i2].propVal;
                        //console.log("matches: " + i2 + " prop: " + prop + " " + val);
                        if (t[i][prop] == val)
                            return t[i];
                    }
                }
            }
        }
        catch (e)
        {
            //console.log("Error while parsing json. Error info: " + e);
            return null;
        }

        return null;
    }

0 answers to this question

Recommended Posts

There have been no answers to this question yet

This topic is now closed to further replies.
  • Posts

    • Dragon's Dogma 2: Dark Arisen expansion to bring snowy region, new updates also coming by Pulasthi Ariyasinghe Capcom had a surprise waiting for Dragon's Dogma fans today in the Nintendo Direct presentation. The company revealed an expansion for the second installment with a name that should be familiar to series veterans. Coming later this year, Dragon's Dogma 2: Dark Arisen is promising a massive new region to explore, new monsters, fresh skills to learn, and more. The studio says players will be heading to the Northern region of the world, named Norgan, to find new secrets about an undying "Fallen Dragon." There will be forgotten relics that the protagonist can find to unlock fresh weapons and skills the expansion is introducing. Players will also be able to find mysterious equipment from a previous Arisen as a part of the expansion, all part of 12 Lost Rites Dungeon Challenges they must complete to gain access. In Neowin's own review, I found Dragon's Dogma 2 to be an impressive RPG when it launched back in 2024, giving the title an 8.5/10 for its class variants, companion system, and immersive exploration. "Once a prosperous region of the kingdom of Vermund, it was abandoned many years ago for reasons unknown," says Capcom about the new region. "Long has it been since any soul traveled its paths. Blanketed in heavy snow, these frigid lands are home to savage hordes and creatures of unbelievable power. Those who are capable of vanquishing such fearsome foes, or those who possess a keen eye for exploration, will find themselves rewarded with powerful relics." Dragon’s Dogma 2: Dark Arisen expansion launches on October 9, 2026, with a $29.99 price tag. Ahead of the expansion release, Capcom is also planning to release two free updates to the base game. The first will land tomorrow, June 10, bringing more accessible fast travel with an Eternal Ferrystone and other quality-of-life adjustments. The second update will land sometime in August, aiming to improve frame rates, add more save slots, and bring even more community-requested adjustments. This expanded Dark Arisen edition is also launching on the Nintendo Switch 2 on the same day the content comes to PC, Xbox Series X|S, and PlayStation 5.
    • Classic themes are just the colors on the bar like the olden days, if you use the image themes, it does fancy transparent backgrounds and it makes the elements of the app look like they are transparent bubbles. This sample image shows what it looks like.  
    • Good point, unfortunately. NextDNS has far more filters and workarounds than uBlock, and it's easy to implement.
  • Recent Achievements

    • Week One Done
      rubentuben8 earned a badge
      Week One Done
    • Week One Done
      ARaclen earned a badge
      Week One Done
    • One Year In
      jojodbn earned a badge
      One Year In
    • One Month Later
      jojodbn earned a badge
      One Month Later
    • Week One Done
      jojodbn earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      524
    2. 2
      PsYcHoKiLLa
      231
    3. 3
      +Edouard
      124
    4. 4
      ATLien_0
      87
    5. 5
      Steven P.
      83
  • Tell a friend

    Love Neowin? Tell a friend!