You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

遍历对象并按指定顺序映射新对象,编写数量构建函数

Hey there! Let's break down and solve these two practical coding tasks for you.

需求一:遍历对象并按指定顺序映射为新对象

问题分析

You want to create a new object from an original one, strictly following a custom order of keys. The core idea is to prioritize extracting key-value pairs in your specified sequence, with optional handling for keys that aren't in your order list.

实现代码示例

function mapObjectByOrder(originalObj, keyOrder) {
  const newObj = {};
  // Iterate through the specified key order to build the new object
  keyOrder.forEach(key => {
    if (originalObj.hasOwnProperty(key)) {
      newObj[key] = originalObj[key];
    }
    // Uncomment below if you want to keep keys from the original object that aren't in the order list
    // else {
    //   newObj[key] = originalObj[key]; // Or set a default value here
    // }
  });
  return newObj;
}

// Example usage
const originalUser = { name: 'Alice', age: 30, city: 'New York' };
const targetKeyOrder = ['age', 'name', 'city'];
const orderedUser = mapObjectByOrder(originalUser, targetKeyOrder);
console.log(orderedUser); // { age: 30, name: 'Alice', city: 'New York' }

Quick Notes

  • The function takes the original object and your desired key order array, then builds the new object in that exact sequence
  • hasOwnProperty ensures we only work with properties directly on the original object (avoids prototype chain properties)
  • If you need to retain extra keys from the original object that aren't in your order list, add an extra loop to append those after processing the specified order

需求二:按循环顺序构建指定数量的变量数组

问题分析

You need a function that takes a number (from 1 up to a total sum, like 7) and returns an array of that length, where elements follow the repeating sequence: variantId:1111 → 2222 → 3333 → 4444 → 1111 (second instance) → 2222 (second instance) → .... Modulo arithmetic is perfect here to handle the repeating pattern.

实现代码示例

function buildVariantArray(count) {
  // Define the base repeating sequence
  const baseSequence = [1111, 2222, 3333, 4444];
  const resultArray = [];
  
  for (let i = 0; i < count; i++) {
    // Use modulo to cycle through the base sequence
    const currentVariantId = baseSequence[i % baseSequence.length];
    resultArray.push({ variantId: currentVariantId });
  }
  
  return resultArray;
}

// Example usage: input 7
const variantList = buildVariantArray(7);
console.log(variantList);
// Output:
// [
//   { variantId: 1111 },
//   { variantId: 2222 },
//   { variantId: 3333 },
//   { variantId: 4444 },
//   { variantId: 1111 },
//   { variantId: 2222 },
//   { variantId: 3333 }
// ]

Quick Notes

  • The baseSequence array holds the values we want to repeat
  • i % baseSequence.length calculates which index of the base sequence to use for each position in the result array, creating the repeating pattern
  • Each iteration creates an object with the variantId and pushes it to the result array, ensuring we end up with exactly count elements

内容的提问来源于stack exchange,提问作者chris cozzens

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:17:03