2

I have two arrays in angular.

One is regular array A with values ['Stock_Number', 'Model', 'Type', 'Bill_Number']

Another is associated array B like the following

0:[
  {
    'Stock_Number': 'GTH738', 
    'Model': 'sample_model', 
    'Type': 'sample_type', 
    'Bill_Number': 7784754,
    'some_prop1': 'prop1_val',
    'some_prop2': 'prop2_val'
  }
];

Both arrays are dynamic. Also B has more columns than A. A has the keys of B's(A is subset of B) I want to get a new array C only for elements in A. For this I'm checking if key exists in B. Following is my code

for(var i=0,j=0; i<B.length,j<A.length; i++,j++){
        if (!B.hasOwnProperty(A)) {
           var value = A[j];
                console.log('if-'+value); //printing value 
                console.log(B[0].value); // printing undefined 
               // C.push(B[0].value);
        }else{
            //some code
        }
    }

Resulting array should be something like the following

{
'Stock_Number': 'GTH738', 
'Model': 'sample_model', 
'Type': 'sample_type', 
'Bill_Number': 7784754
}

Can somebody suggest me how this can be achieved?

5
  • Could you clarify, in B, does it look like: { 0: [{'Stock_Number' ...? The 0 is the property? Commented Mar 27, 2018 at 7:03
  • What is your desired structure for array C? Commented Mar 27, 2018 at 7:03
  • yeah, actually it's a JSON array getting through API result. It's the index Commented Mar 27, 2018 at 7:04
  • @Ankit it would be as associated array just like B with stock number and related value Commented Mar 27, 2018 at 7:05
  • please select an example where the wanted result (please add that as well) is kind of possible. Commented Mar 27, 2018 at 7:08

4 Answers 4

1

You can use this approach. For better example on what you need I have added some extra properties on object of B array which is not in array A. Thus, those properties are excluded from the objects in array C

var A = ['Stock_Number', 'Model', 'Type', 'Bill_Number'];
var B = [
  {
    'Stock_Number': 'GTH738', 
    'Model': 'sample_model', 
    'Type': 'sample_type', 
    'Bill_Number': 7784754
  },
  {
    'Stock_Number': 'GTH740', 
    'Model': 'sample_model2', 
    'Type': 'sample_type2', 
    'Bill_Number': 7784754,
    'someProp1': 1,
    'someProp2': 2,
  }
];

var C = [];
for(var i=0; i<B.length; i++){
  var obj = B[i];
  var objKeys = Object.keys(obj);
  var resObj = {};
  A.forEach(function(itemA){
    if(objKeys.indexOf(itemA) !== -1){
      resObj[itemA] = obj[itemA];
    }
  });
  C.push(resObj);
}

console.log(C);

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

3 Comments

B has only one object, not multiple objects. I edited my B it would look like mine
includes keyword doesn't work in angular. Any alternative?
you can use indexOf. I have updated my answer using that. @Esco
0

Just replace the line

console.log(B[0].value); 

with

console.log(B[0][value]);

1 Comment

It doesn't allow that way
0

You could filter the unknown keys and build a new object out of the surplus keys.

var arrayA = ['Stock_Number', 'Model', 'Type', 'Bill_Number'],
    arrayB = [{ Stock_Number: 'GTH738', Model: 'sample_model', Type: 'sample_type', Bill_Number: 7784754 }, { Stock_Number: 'GTH738', Model: 'sample_model', Type: 'sample_type', Bill_Number: 7784754, foo: 'bar', baz: 42 }],
    result = [];

arrayB.forEach(function (b) {
    keys = Object.keys(b).filter(k => !arrayA.includes(k));
    if (keys.length) {
        result.push(Object.assign(...keys.map(k => ({ [k]: b[k] }))));
    }
});

console.log(result);

Comments

0
var A = ['Stock_Number', 'Model', 'Type', 'Bill_Number'];
var B = [
  {
    'Stock_Number': 'GTH738', 
    'Model': 'sample_model', 
    'Type': 'sample_type', 
    'Bill_Number': 7784754
  },
  {
    'Stock_Number': 'GTH740', 
    'Model': 'sample_model2', 
    'Type': 'sample_type2', 
    'Bill_Number': 7784754,
    'someProp1': 1,
    'someProp2': 2,
  }
];

var C = B.map(item =>{
    let obj = {};
    A.forEach(key => {
        obj[key] = item[key];
    });
    return obj;
});

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.