S
S
shading2020-05-08 20:16:39
JavaScript
shading, 2020-05-08 20:16:39

Redefine fields on an object?

Hello.
There is an object with fields

"BIRZHASP":[
      {
         "Code":"1",
         "ForAbonentType":"ip"
      },
      {
         "Code":"2",
         "ForAbonentType":"ul"
      }
   ],
   "BIRZHASPF":[
      {
         "Code":"3",
         "ForAbonentType":"ip"
      },
      {
         "Code":"4",
         "ForAbonentType":"ul"
      }
   ],


There is also an array that stores card data
[
{
  serviceTypes: ['BIRZHASP', 'BIRZHASPF']
  qualNames: {
       ip: какой то код
       ul: какой-то код
   };
},
и так далее карточки с такими же данными
]


It is necessary to change the fields of qualNames for cards so that the data is taken from the object above, while qualNames should be changed only for those cards whose serviceTypes are equal to the field names from the top object.

I broke my head and I can't figure out how to do it anymore)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Shamanov, 2020-05-08
@SilenceOfWinter

https://developer.mozilla.org/en/docs/Web/JavaScript...

E
Eugene, 2020-05-08
@Kasperenysh

let obj = {
  "BIRZHASP":[
      {
         "Code":"1",
         "ForAbonentType":"ip"
      },
      {
         "Code":"2",
         "ForAbonentType":"ul"
      }
   ],
   "BIRZHASPF":[
      {
         "Code":"3",
         "ForAbonentType":"ip"
      },
      {
         "Code":"4",
         "ForAbonentType":"ul"
      }
   ]
};

let arr = [
  {
    serviceTypes: ['BIRZHASP', 'BIRZHASPF']
    qualNames: {
       ip: какой то код
       ul: какой-то код
     };
  }
];

arr.foreach(elem => {
  elem.serviceTypes.forech(e => {
    if (obj.hasOwnProperty(e)) {
      elem.qualNames.ip = obj[e][0].ForAbonentType;
      elem.qualNames.ul = obj[e][1].ForAbonentType;
    }
  });
});

If I understand correctly, then something like this))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question