从对象数组中提取值
Extract value from the array of objects
我在一个变量中有一个对象数组,在另一个变量中有提供者的 ID。如何根据 id 获取提供者的名称。如何根据 ID 获取名称。当我将 id 变量与包含对象数组的变量进行比较时。
对象数组:
0: Object { id: "620d77165bd6857e3bdfed46", name: "provider 2", type: "TMS" }
1: Object { id: "620d771a5bd6857e3bdfed49", name: "provider 3", type: "Other" }
2: Object { id: "620d77205bd6857e3bdfed4c", name: "provider 4", type: "Load Board" }
3: Object { id: "62220e49c547d431a94c2aa8", name: "provider 5", type: "TMS" }
4: Object { id: "62221b86c547d431a94c2aba", name: "provider 6", type: "Load Board" }
5: Object { id: "6226887428359eb63456901a", name: "provider 1", type: "Load Board" }
6: Object { id: "62309b7d75a9fe3632ed9649", name: "asd", type: "TMS" }
7: Object { id: "62309b8375a9fe3632ed964f", name: "asdasdas", type: "Load Board" }
8: Object { id: "6230de7eb8432ab865c77a04", name: "asd provider 2", type: "Load Board" }
9: Object { id: "6230e05fb8432ab865c77a09", name: "adad", type: "Load Board" }
10: Object { id: "6230e091b8432ab865c77a10", name: "adada123123", type: "Load Board" }
11: Object { id: "6230e0e8b8432ab865c77a19", name: "asdasdasdadasdasda", type: "Load Board" }
12: Object { id: "6230e22db8432ab865c77a20", name: "adasd", type: "Load Board" }
13: Object { id: "6230e239b8432ab865c77a29", name: "adasd12312313123", type: "TMS" }
14: Object { id: "623366f8fc1ac7ff6f7bbd17", name: "asasasaaaaaaaaaaaa1111111111", type: "Load Board" }
15: Object { id: "6233670dfc1ac7ff6f7bbd25", name: "asasasaa1212111111", type: "Select a Type" }
16: Object { id: "62337769dac5619a6c6f56f9", name: "simpleProovider", type: "newTMS" }
17: Object { id: "6241a25e5bc31aec9152932f", name: "Uber Frieght", type: "Load Board" }
到目前为止我尝试了什么。
let result = providerList.map(({ id }) => name);
console.log(result);
您可以使用 find
(see MDN) 在对象数组中搜索特定值。
const result = providerList.find((e) => e.id === "id_to_look_for");
有用的文档链接
你必须为此使用过滤器
const result = providerList.filter((x) => x.id === yourId);
或者你甚至可以使用 find , find 和 filter 的区别在于 filter 可以用来查找满足条件 e.id === yourId
的多个值
而 find 可用于查找唯一值
const result = providerList.find((x) => x.id === yourId);
您可以使用find
函数。
const result = providerList.find((x) => x.id === yourId);
const name = result? result.name : null;
或者您可以通过 lodash
库获取它。
const name = _.get(_.find(providerList, {id: yourId}), 'name');
我在一个变量中有一个对象数组,在另一个变量中有提供者的 ID。如何根据 id 获取提供者的名称。如何根据 ID 获取名称。当我将 id 变量与包含对象数组的变量进行比较时。
对象数组:
0: Object { id: "620d77165bd6857e3bdfed46", name: "provider 2", type: "TMS" }
1: Object { id: "620d771a5bd6857e3bdfed49", name: "provider 3", type: "Other" }
2: Object { id: "620d77205bd6857e3bdfed4c", name: "provider 4", type: "Load Board" }
3: Object { id: "62220e49c547d431a94c2aa8", name: "provider 5", type: "TMS" }
4: Object { id: "62221b86c547d431a94c2aba", name: "provider 6", type: "Load Board" }
5: Object { id: "6226887428359eb63456901a", name: "provider 1", type: "Load Board" }
6: Object { id: "62309b7d75a9fe3632ed9649", name: "asd", type: "TMS" }
7: Object { id: "62309b8375a9fe3632ed964f", name: "asdasdas", type: "Load Board" }
8: Object { id: "6230de7eb8432ab865c77a04", name: "asd provider 2", type: "Load Board" }
9: Object { id: "6230e05fb8432ab865c77a09", name: "adad", type: "Load Board" }
10: Object { id: "6230e091b8432ab865c77a10", name: "adada123123", type: "Load Board" }
11: Object { id: "6230e0e8b8432ab865c77a19", name: "asdasdasdadasdasda", type: "Load Board" }
12: Object { id: "6230e22db8432ab865c77a20", name: "adasd", type: "Load Board" }
13: Object { id: "6230e239b8432ab865c77a29", name: "adasd12312313123", type: "TMS" }
14: Object { id: "623366f8fc1ac7ff6f7bbd17", name: "asasasaaaaaaaaaaaa1111111111", type: "Load Board" }
15: Object { id: "6233670dfc1ac7ff6f7bbd25", name: "asasasaa1212111111", type: "Select a Type" }
16: Object { id: "62337769dac5619a6c6f56f9", name: "simpleProovider", type: "newTMS" }
17: Object { id: "6241a25e5bc31aec9152932f", name: "Uber Frieght", type: "Load Board" }
到目前为止我尝试了什么。
let result = providerList.map(({ id }) => name);
console.log(result);
您可以使用 find
(see MDN) 在对象数组中搜索特定值。
const result = providerList.find((e) => e.id === "id_to_look_for");
有用的文档链接
你必须为此使用过滤器
const result = providerList.filter((x) => x.id === yourId);
或者你甚至可以使用 find , find 和 filter 的区别在于 filter 可以用来查找满足条件 e.id === yourId
的多个值
而 find 可用于查找唯一值
const result = providerList.find((x) => x.id === yourId);
您可以使用find
函数。
const result = providerList.find((x) => x.id === yourId);
const name = result? result.name : null;
或者您可以通过 lodash
库获取它。
const name = _.get(_.find(providerList, {id: yourId}), 'name');