Home > Blockchain >  How to return the data in one function call for two different object keys
How to return the data in one function call for two different object keys

Time:02-24

I have an object which looks like this :

const data = {
  students: [{
    code: '1',
    number: '22',
    type: 'regular',
    name: 'john',
    age: '11',
    class: 'A',
  }, {
    code: '2',
    number: '23',
    type: 'regular',
    name: 'steve',
    age: '12',
    class: 'B',
  }],
  teachers: [{
    code: '22',
    number: '101',
    type: 'intern',
    name: 'mac',
  }, {
    code: '23',
    number: '102',
    type: 'perm',
    name: 'jess',
  }],
};

It has different keys and values.

Here, I am trying to massage this data so that I can obtain the following result: So I am trying to get an array which will have only students data and other which will have teachers data from one function itself.

const result1 = [{
  code: '1',
  number: '22',
  type: 'regular',
  name: 'john',
}, {
  code: '2',
  number: '23',
  type: 'regular',
  name: 'steve',
}];
const result2 = [{
  code: '22',
  number: '101',
  type: 'intern',
  name: 'mac',
}, {
  code: '23',
  number: '102',
  type: 'perm',
  name: 'jess',
}];

what I tried is :

const getData = ({data = []}) => {
 data?.map({ code,
number, 
regular, 
name } ) => {
return{
code,
number, 
regular, 
name
}}
}

getData(data.students)
getData(data.teachers)   // How do get this data in one function call itself

This gives me the result , but for this I need to call this function twice once for students and one for teachers. I want to call this function once.

Thanks

CodePudding user response:

You could map new entries from the object and take the mapped new structure.

const
    data = { students: [{ code: '1', number: '22', type: 'regular', name: 'john', age: '11', class: 'A' }, { code: '2', number: '23', type: 'regular', name: 'steve', age: '12', class: 'B' }], teachers: [{ code: '22', number: '101', type: 'intern', name: 'mac' }, { code: '23', number: '102', type: 'perm', name: 'jess' }] },
    getData = ({ code, number, regular, name }) => ({ code, number, regular, name }),
    result = Object.fromEntries(Object
        .entries(data)
        .map(([k, v]) => [k, v.map(getData)])
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

CodePudding user response:

I'm not sure what use there is in this, but since you want to run a function twice and get two results, do that, and combine into an object:

const data = {
  students: [{
    code: '1',
    number: '22',
    type: 'regular',
    name: 'john',
    age: '11',
    class: 'A'
  }, {
    code: '2',
    number: '23',
    type: 'regular',
    name: 'steve',
    age: '12',
    class: 'B'
  }],
  teachers: [{
    code: '22',
    number: '101',
    type: 'intern',
    name: 'mac'
  }, {
    code: '23',
    number: '102',
    type: 'perm',
    name: 'jess'
  }]
};
const transformData = (data = []) =>
  data.map(({
    code,
    number,
    regular,
    name
  }) => ({
    code,
    number,
    regular,
    name
  }));
const getData = (data) =>
  ({
    students: transformData(data.students),
    teachers: transformData(data.teachers)
  });
console.log(getData(data));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Note: I modified the transformData function to remove some extra syntax, and to remove the optional chaining since the Stack Snippets' ancient version of Babel doesn't support it.

Also, there's no property named regular on the original objects in the array, so they come out undefined.

CodePudding user response:

Since from the provided data, it looks like the OP wants to mainly map a certain set of properties of student/teacher items from a map/object, a possible approach is to reduce the data object's entries and apply the mapping exclusively for a matching students or teachers key each referring to a valid array-type value.

const data = {
  students: [{ code: "1", number: "22", type: "regular", name: "john", age: "11", class: "A" }, { code: "2", number: "23", type: "regular", name: "steve", age: "12", class: "B" }],
  teachers: [{ code: "22", number: "101", type: "intern", name: "mac" }, { code: "23", number: "102", type: "perm", name: "jess" }],
};

const {

  students: result1,
  teachers: result2,

} = Object
  .entries(data)
  .reduce((result, [key, value]) => {
    if (
      // process only for a matching key ...
      (/^students|teachers$/).test(key)

      // ... and a valid value type.
      && Array.isArray(value)
    ) {
      result[key] = value
        .map(({ code, number, type, name }) =>
          ({ code, number, type, name })
        );
    }
    return result
  }, {});

console.log({ result1, result2 });
.as-console-wrapper { min-height: 100%!important; top: 0; }

CodePudding user response:

Based on the input and output looks like you're just trying to slice off the keys age and class from the objects. So you'd just need to iterate through both the keys then the underlying objects to remove them.

const getData = (data) => {
  Object.keys(data).forEach(function callback(key) {
    let value = data[key];

    value.forEach(function callback(obj) {
      let blockArray = ['age', 'class'];
      blockArray.forEach(e => delete obj[e]);
    });
  });

  return data;
}

const updatedData = getData(data);

const result1 = updatedData.students;
console.dir(result1);

const result2 = updatedData.teachers;
console.dir(result2);
  • Related