0

I have the below array of objects. Now I need to filter and create 3 separate array of objects based on the key value.

"data": [
{
  "BaseId": 1,
  "BaseDesc": "18 BHC Baseline",
  "StressId": 5,
  "StressDesc": "Desc 1",
  "bopId": 8,
  "bopDesc": "BOP Desc 1",
},
{
  "BaseId": 1,
  "BaseDesc": "Baseline 2",
  "StressId": 2,
  "StressDesc": "Desc 12",
  "bopId": 8,
  "bopDesc": "BOP Desc 2"
},
{
  "BaseId": 2,
  "BaseDesc": "Baseline 3",
  "StressId": 7,
  "StressDesc": "Desc 3",
  "bopId": 10,
  "bopDesc": "BOP Desc 3"
}

]

Now I need to filter this and create 3 separate array of object such that:

1. BaseData Array Obj


"baseData": [
{
  "BaseId": 1,
  "BaseDesc": "18 BHC Baseline"
},
{
  "BaseId": 1,
  "BaseDesc": "Baseline 2"
},
{
  "BaseId": 2,
      "BaseDesc": "Baseline 3"
    }
  ]
  1. Stress Array Obj:

        "data": [
        {
          "StressId": 5,
          "StressDesc": "Desc 1"
        },
        {
          "StressId": 2,
          "StressDesc": "Desc 12"
        },
        {
          "StressId": 7,
          "StressDesc": "Desc 3"
        }
      ]
    

And similarly 3rd object for bop data. So if my actual array contains "base*" in the keys, then I need to filter and add to bop Array and same logic goes for other two array of objects.

Can someone please guide me how to achieve this.

3 Answers 3

1

How about something like:

const data = [
  {
    BaseId: 1,
    BaseDesc: "18 BHC Baseline",
    StressId: 5,
    StressDesc: "Desc 1",
    bopId: 8,
    bopDesc: "BOP Desc 1"
  },
  {
    BaseId: 1,
    BaseDesc: "Baseline 2",
    StressId: 2,
    StressDesc: "Desc 12",
    bopId: 8,
    bopDesc: "BOP Desc 2"
  },
  {
    BaseId: 2,
    BaseDesc: "Baseline 3",
    StressId: 7,
    StressDesc: "Desc 3",
    bopId: 10,
    bopDesc: "BOP Desc 3"
  }
];

const pickSpecifiedProperties = (startingPropString, arrayOfObjects) =>
  arrayOfObjects.map(obj => {
    const targetKeys = Object.keys(obj).filter(
      keyName =>
        keyName.toLowerCase().indexOf(startingPropString.toLowerCase()) === 0
    );
    return targetKeys.reduce((data, keyName) => {
      const newProperty = { [keyName]: obj[keyName] };
      return { ...data, ...newProperty };
    }, {});
  });

const baseData = pickSpecifiedProperties("Base", data);
const stressData = pickSpecifiedProperties("Stress", data);
const bopData = pickSpecifiedProperties("bop", data);

console.log({ baseData, stressData, bopData });
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much XDgg. This is more robust. Appreciate your help!!
My pleasure @Sriks :)
0

const data = [
{
  "BaseId": 1,
  "BaseDesc": "18 BHC Baseline",
  "StressId": 5,
  "StressDesc": "Desc 1",
  "bopId": 8,
  "bopDesc": "BOP Desc 1",
},
{
  "BaseId": 1,
  "BaseDesc": "Baseline 2",
  "StressId": 2,
  "StressDesc": "Desc 12",
  "bopId": 8,
  "bopDesc": "BOP Desc 2"
},
{
  "BaseId": 2,
  "BaseDesc": "Baseline 3",
  "StressId": 7,
  "StressDesc": "Desc 3",
  "bopId": 10,
  "bopDesc": "BOP Desc 3"
}
];

const getData = (array, prefix) => array.map(item => ({ [prefix + 'Id']: item[prefix + 'Id'], [prefix + 'Desc']: item[prefix + 'Desc'] }));

console.log(getData(data, 'Base'));
console.log(getData(data, 'Stress'));
console.log(getData(data, 'bop'));

1 Comment

Thank you so much Adrain
0

This should work for you. You can also use jQuery.map().

var data = [
{
  "BaseId": 1,
  "BaseDesc": "18 BHC Baseline",
  "StressId": 5,
  "StressDesc": "Desc 1",
  "bopId": 8,
  "bopDesc": "BOP Desc 1",
},
{
  "BaseId": 1,
  "BaseDesc": "Baseline 2",
  "StressId": 2,
  "StressDesc": "Desc 12",
  "bopId": 8,
  "bopDesc": "BOP Desc 2"
},
{
  "BaseId": 2,
  "BaseDesc": "Baseline 3",
  "StressId": 7,
  "StressDesc": "Desc 3",
  "bopId": 10,
  "bopDesc": "BOP Desc 3"
}];

var baseArray = [];
var stressArray = [];

$.each(data, function(idx, val) {

//Handle BaseArray stuff here.
if($.grep(baseArray, function(baseVal){
  return baseVal.BaseId === val.BaseId && baseVal.BaseDesc === val.BaseDesc;
}).length === 0){
  baseArray.push({BaseId: val.BaseId, BaseDesc: val.BaseDesc});
}

//Handle StressArray stuff here.
if($.grep(stressArray, function(stressVal){
  return stressVal.StressId === val.StressId && stressVal.StressDesc === val.StressDesc;
}).length === 0){
  stressArray.push({StressId: val.StressId, StressDesc: val.StressDesc});
}

});

console.log(baseArray);
console.log(stressArray);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

1 Comment

I wouldn't recommend using jQuery unless your project is already including it.

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.