如何 return 一个函数调用中的数据用于两个不同的对象键
How to return the data in one function call for two different object keys
我有一个看起来像这样的对象:
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 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',
}];
我试过的是:
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
这给了我结果,但为此我需要为学生调用此函数两次,为教师调用一次。我想调用一次这个函数。
谢谢
您可以映射对象中的新条目并采用映射的新结构。
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; }
根据输入和输出,您似乎只是想从对象中切出键 age
和 class
。因此,您只需要遍历这两个键,然后遍历底层对象即可删除它们。
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);
我不确定这有什么用,但既然你想 运行 一个函数两次并得到两个结果,那么这样做,然后组合成一个对象:
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; }
注意:我修改了 transformData
函数以删除一些额外的语法,并删除了可选链接,因为 Stack Snippets 的古老版本的 Babel 不支持它。
此外,数组中的原始对象没有名为 regular
的 属性,因此它们未定义。
从提供的数据来看,OP 似乎希望 主要 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
并将映射专门用于每个匹配的 students
或 teachers
键引用有效的 array-type 值。
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; }
我有一个看起来像这样的对象:
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 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',
}];
我试过的是:
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
这给了我结果,但为此我需要为学生调用此函数两次,为教师调用一次。我想调用一次这个函数。
谢谢
您可以映射对象中的新条目并采用映射的新结构。
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; }
根据输入和输出,您似乎只是想从对象中切出键 age
和 class
。因此,您只需要遍历这两个键,然后遍历底层对象即可删除它们。
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);
我不确定这有什么用,但既然你想 运行 一个函数两次并得到两个结果,那么这样做,然后组合成一个对象:
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; }
注意:我修改了 transformData
函数以删除一些额外的语法,并删除了可选链接,因为 Stack Snippets 的古老版本的 Babel 不支持它。
此外,数组中的原始对象没有名为 regular
的 属性,因此它们未定义。
从提供的数据来看,OP 似乎希望 主要 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
并将映射专门用于每个匹配的 students
或 teachers
键引用有效的 array-type 值。
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; }