0

I am trying to use linq.js to match a locate a object by a property. the property i need to match is in a object of arrays and then nested in array of arrays.

Json i am looping through

customSeries = [{"name":"Chantal Hamlet - Green Castle Homes","subId":"10223","bldId":"13551","data":[[179900,1386],[214900,1440],[194500,1496],[217900,1504],[189900,1542],[184900,1546],[192500,1570],[189900,1576],[191900,1598],[204900,1626],[219900,1651],[212900,1704],[214900,1787],[219900,1837],[224900,1857]],"removeByNames":[["null"],["null"],["null"],["null"],["null"],["null"],["null"],["null"],["null"],["null"],["null"],["null"],["null"],["null"],["null"]]},{"name":"Ella Sea Condos - Sahnow Construction","subId":"9761","bldId":"27380","data":[[199900,1500]],"removeByNames":[["null"]]},{"style":"smooth","color":"blue","data":[[20000,200],[40000,400],[[40000,400]],[30000,300],[[30000,300]]],"name":"Subject Property","removeByNames":[["Product1"],["Product2"],["Product3"]]}]

item to match

var modelName = 'Product2'

javascript

  remove: function (e) {
            removeByNames = []
            var modelName = e.model.name;

            // Enumerate through the series
            var customSeriesSearchResults = Enumerable.From(customSeries)
                .Where(function (item) {
                    // Enumerate through the series.removeByNames
                    return Enumerable.From(item.removeByNames).Any(function (modelName) {

                        // Find matching removeByNames.name
                        return Enumerable.From(removeByNames).Contains(modelName);
                })
            })
                .ToArray();

    }
5
  • customersWithProduct2 = customSeries.filter(function(customer){ return customer.modelname === 'Product2';}) Commented May 20, 2015 at 18:50
  • Your removeByNames array is mixed with arrays of string and arrays of arrays of strings... you need to flatten that down or something. Commented May 20, 2015 at 18:56
  • @agconti The property i need to match with modelName is located in removeByNames Commented May 20, 2015 at 19:03
  • @JeffMercado where would i flatten it out? Commented May 20, 2015 at 19:03
  • 1
    Ideally it would be cleaned up at the source of the data. To me, a lot of the data is garbage there. It needs to be cleaned up. That's not the client code's responsibility. Commented May 20, 2015 at 20:18

1 Answer 1

1

With pure js you could loop through every object and then do another loop over the removedByNames array. Then use array.indexOf() on the flattened array, it will return the position of the string in array if found. If nothing is found it will return -1.

You can use the same approach with linqJs. Not sure if there is a better way to do it in linqjs. But it also works with linq.

Please have a look at the demo code below and here at jsfiddle. (I've added dummy data to your json to have a second object with the same modelName.)

customSeries = [{
    "name": "Chantal Hamlet - Green Castle Homes",
        "subId": "10223",
        "bldId": "13551",
        "data": [
        [179900, 1386],
        [214900, 1440],
        [194500, 1496],
        [217900, 1504],
        [189900, 1542],
        [184900, 1546],
        [192500, 1570],
        [189900, 1576],
        [191900, 1598],
        [204900, 1626],
        [219900, 1651],
        [212900, 1704],
        [214900, 1787],
        [219900, 1837],
        [224900, 1857]
    ],
        "removeByNames": [
        ["null"],
        ["null"],
        ["null"],
        ["null"],
        ["null"],
        ["null"],
        ["null"],
        ["null"],
        ["null"],
        ["null"],
        ["null"],
        ["null"],
        ["null"],
        ["null"],
        ["null"]
    ]
}, {
    "name": "Ella Sea Condos - Sahnow Construction",
        "subId": "9761",
        "bldId": "27380",
        "data": [
        [199900, 1500]
    ],
        "removeByNames": [
        ["null"]
    ]
}, {
    "style": "smooth",
        "color": "blue",
        "data": [
        [30000, 500],
        [40000, 400],
        [
            [40000, 400]
        ],
        [50000, 800],
        [
            [50000, 800]
        ]
    ],
        "name": "Subject Property",
        "removeByNames": [
        ["Product1"],
        ["Product2"],
        [
            ["Product2"]
        ],
        ["Product3"],
        [
            ["Product3"]
        ]
    ]
}, { // add another item with product2
    "style": "smooth",
        "color": "gray",
        "data": [
        [30000, 500],
        [40000, 400],
        [
            [40000, 400]
        ],
        [50000, 800],
        [
            [50000, 800]
        ]
    ],
        "name": "Subject Property dummy data",
        "removeByNames": [
        [
            ["Product2"]
        ],
        ["Product3"],
        [
            ["Product3"]
        ]
    ]
}];


console.log(customSeries);

/*customersWithProduct2 = customSeries.filter(function(customer){ 
    console.log('Cust', customer);
    return customer.modelname === 'Product2';})*/
var modelName = 'Product2'
var customers = [];


// flatten code from here http://stackoverflow.com/questions/6032878/is-there-an-easy-way-to-make-nested-array-flat
var flatten = function (arr) {
    return arr.reduce(function (prev, cur) {
        var more = [].concat(cur).some(Array.isArray);
        return prev.concat(more ? flatten(cur) : cur);
    }, []);
};

//console.log('flat test', flatten(['dummy', ['1','2'], 'dummy2']));
//console.log('flat test', flatten([['1']]));

// with-out linqjs
customSeries.forEach(function (obj) {
    //console.log(obj);
    var foundItem, flattened;

    obj.removeByNames.some(function (name) {
        flattened = flatten(name);

        //console.log('name', name, flattened, flattened.indexOf(modelName) > -1, obj);
        foundItem = flattened.indexOf(modelName) > -1 ? obj : undefined;
        return !!foundItem; //!! creates bool if true exits the loop
    });

    //console.log('found', foundItem);
    if (foundItem) {
        customers.push(foundItem);
    }
});

console.log('pure js', customers);

$('body').append($('<pre/>').html(JSON.stringify(customers, null, 2))); // jquery just to log the object to the output
               
// with linqjs
removeByNames = []
var flatArray = [];

// Enumerate through the series
var customSeriesSearchResults = Enumerable.From(customSeries)
    .Where(function (item) {
    // Enumerate through the series.removeByNames
    return Enumerable.From(item.removeByNames).Any(function (name) {

        // Find matching removeByNames.name
        // console.log('loop', Enumerable.From(item.removeByNames)
        // ,Enumerable.From(removeByNames).Contains(modelName));
        flatArray = flatten(Enumerable.From(item.removeByNames).ToArray());
        return flatArray.indexOf(modelName) > -1; //Enumerable.From(flatArray).Contains(modelName);
    })
}).ToArray();

console.log('with linqjs', customSeriesSearchResults);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/linq.min.js"></script>

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

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.