Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
95 views
in Technique[技术] by (71.8m points)

javascript - Iterate between two numbers with object.keys


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Filter can get you there also:

const obj = { 
    '#1 List title': [{
        title: 'First',
      },
    ],
    '#2 List title': [{
        title: 'Sec', 
      },
    ], 
    '#3 List title': [{
        title: 'Third',
      },
    ], 
    '#4 List title': [{
        title: 'Fourth',
      },
    ],    
    '#5 List title': [{
        title: 'Fifth',
      },
    ],    
  };
  
  const start = 2;
  const end = 4;

const listItem =  Object.keys(obj).filter((_,i) => i >=start && i <=end).map((key) => `
    <div key=${key}>
      <div>${key}</div>
      <div>
        ${obj[key].map(el =>
          {el.title}
        )}
      </div>
    </div>
    `
  )
  
  console.log(listItem)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...