0

I have an array of objects like this:

var bridges = {"countyCd:15
createdDate:"0001-01-01T00:00:00"
createdUserId:0
createdUserIdZ:null
createdUserName:null
description:"SR [email protected] RR COMP OF IND"
districtId:null
encryptedId1:null
encryptedId2:null
isDirty:false
modelState:null
nbiNumber:10
routeNbr:"1"
routeTypeCd:"SR"
transactionType:null
updatedDate:"0001-01-01T00:00:00"
updatedUserId:0
updatedUserIdZ:null
updatedUserName:null", [...]....}

I have another array like this

[countyCd, nbiNumber]

How can create another array keeping just two properties so it becomes like

bridges = {"countyCd:15 nbiNumber:10" , [...]....}

Basically, I am looking for a way to create a function that takes a data array and a filter array as parameters and filters the data array based on the filter array.

Any pointers to this will be much appreciated.

2
  • Is bridges actually an array, or is it an object like you posted? The solution would be different based on which it is Commented Aug 3, 2016 at 22:18
  • @RobM.Bridges is an Array of objects. I am sorry about the ambiguity in my question. Commented Aug 3, 2016 at 22:20

2 Answers 2

3

One solution would be to map over each record and reduce your filter array into an object containing the target proprties:

var bridges = [{
    countyCd:15,
    createdDate:"0001-01-01T00:00:00",
    createdUserId:0,
    createdUserIdZ:null,
    createdUserName:null,
    description:"SR [email protected] RR COMP OF IND",
    districtId:null,
    encryptedId1:null,
    encryptedId2:null,
    isDirty:false,
    modelState:null,
    nbiNumber:10,
    routeNbr:"1",
    routeTypeCd:"SR",
    transactionType:null,
    updatedDate:"0001-01-01T00:00:00",
    updatedUserId:0,
    updatedUserIdZ:null,
    updatedUserName:null
}, {
    countyCd:23,
    createdDate:"0001-01-01T00:00:00",
    createdUserId:0,
    createdUserIdZ:null,
    createdUserName:null,
    description:"SR [email protected] RR COMP OF IND",
    districtId:null,
    encryptedId1:null,
    encryptedId2:null,
    isDirty:false,
    modelState:null,
    nbiNumber:10,
    routeNbr:"1",
    routeTypeCd:"SR",
    transactionType:null,
    updatedDate:"0001-01-01T00:00:00",
    updatedUserId:0,
    updatedUserIdZ:null,
    updatedUserName:null
}];

var filters = ['countyCd', 'nbiNumber'];
var transformedRecords = bridges.map(bridge => filters.reduce((p, c) => { 
    p[c] = bridge[c];
    return p; 
}, {}));
console.log(transformedRecords);
Sign up to request clarification or add additional context in comments.

Comments

1

Say you have an array of bridges, call it bA:

var bA = []; //bridges array
var nbiA = []; // nbia array with countyCd
var newA = []; // new array
bA.forEach(function(element, index, array){
    var newEntry = {
        'countyCd':element.countyCd,
        'nbiNumber':nbiA.find(function(nbi){
            return nbi[countyCd] == element.countyCd;
        }).nbiNumber
    };
    newA.push(newEntry);
});
//do whatever you want with the newA array

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.