当前位置:网站首页>Js a simple way to store several objects in an array
Js a simple way to store several objects in an array
2022-07-25 02:39:00 【Large American style without sugar】
Demand analysis
I have an array arr, To store four objects like the following into an array :
{
name: ' Xiao Huang ',
age: 23
}
reflection
What is the general first thought ? Manually create the object shown above , Then one by one push Into array
let arr = [];
const obj1 = {
name: ' Xiao Huang ',
age: 23
};
const obj2 ={
name: ' Xiao Wang ',
age: 20
};
const obj3 = {
name: ' Xiao Shen ',
age: 18
};
const obj4 = {
name: ' Xiao Song ',
age: 11
};
arr.push(obj1);
arr.push(obj2);
arr.push(obj3);
arr.push(obj4);
Problem analysis
The way it's written is ok Do you ? That is, of course, ok, But the problem is that there is too much code , If there are hundreds of objects , Isn't it necessary to manually create hundreds of objects .
Optimize
It can be optimized by the following code :
let arr = [];
const obj = {
' Xiao Huang ': 23,
' Xiao Wang ': 20,
' Xiao Shen ': 18,
' Xiao Song ': 11
};
for (const key in obj) {
const param = {
};
param['name'] = key;
param['age'] = obj[key];
arr.push(param);
}
// or
Object.keys(obj).forEach(ele => {
const param = {
};
param['name'] = ele;
param['age'] = obj[ele];
arr.push(param);
})
shortcoming
Only objects within two key value pairs are supported
边栏推荐
- Using the artifact typora+picgo, Youdao cloud can automatically upload pictures in markdown without members
- Server performance monitoring
- Four redis cluster schemes you must know and their advantages and disadvantages
- Tp5.1 include include files (reference public files)
- In the post deep learning era, where is the recommendation system going?
- Why can't reading more books improve your writing?
- Easy to master SSO single sign on, see this article
- After upgrading v2.1.0, the synchronization failed
- Master jedispoolconfig parameter configuration and learn tuning skills
- Apk packaging process
猜你喜欢

English grammar_ Reflexive pronoun

Solution to the occupation of project startup port

How to judge which star you look like?

Server performance monitoring

Mgre.hdlc.ppp.chap.nat comprehensive experiment

Is redis'module'not an internal or external command?

My creation anniversary (3rd Anniversary)

Project management tool Zen

UDP message structure and precautions

Example demonstration of "uncover the secrets of asp.net core 6 framework" [02]: application development based on routing, MVC and grpc
随机推荐
Execution methods with static code blocks and child and parent classes
Object.defineproperty use
Consul cluster deployment
Google launched another "man grabbing war" for core making, and Intel's 17 year veteran joined!
"Introduction to interface testing" punch in to learn day07: websocket interface: how to test a completely unfamiliar protocol interface?
Several dpdk control frameworks
Redis unauthorized access vulnerability recurrence (www.hetianlab.com)
Tp5.0 background admin access
Simulation Implementation of [STL] string class
Hongmeng harmonyos 3 official announcement: officially released on July 27; Apple slowed down the recruitment and expenditure of some teams in 2023; Russia fined Google 2.6 billion yuan | geek headlin
StrError and PERROR
Using the artifact typora+picgo, Youdao cloud can automatically upload pictures in markdown without members
ICO objects in classification
[C language] program compilation (preprocessing)
How to use blender to make 360 degree panorama and panoramic video?
Tp5.1 initialize initialization method (not \u initialize)
Visualization of correlation coefficient matrix
Summary thinking caused by the function of a SMS verification code [easy to understand]
Anacona has too many environments?? How to view your current environment in jupyter
[pyGame practice] nostalgic classic - do you remember the name of this chess game for children? (must collect)