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

如何访问含唯一命名子数组的JSON值并按update_time排序

Solution: Sort Products by update_time Across All Departments

Got it, let's work through this problem step by step. The core issue here is that your Products properties are plain JavaScript objects (not arrays), so we can't directly apply the sort() method to them. We need to first convert these objects into arrays of product entries, sort those arrays by the update_time values, and then we can either work with the sorted array or convert it back to an object if needed.

Step 1: Collect All Product Entries

First, we'll gather every product from both departments, keeping track of their unique keys and associated data:

const obj = { "company": { "department_1": { "Products": { "unique_keyname_1": { "product_owner": "co-worker-1", "update_time": "unix_timestamp_1" }, "unique_keyname_5": { "product_owner": "co-worker-4", "update_time": "unix_timestamp_45" }, "unique_keyname_8": { "product_owner": "co-worker-2", "update_time": "unix_timestamp_5" } } }, "department_2": { "Products": { "unique_keyname_3": { "product_owner": "co-worker-1", "update_time": "unix_timestamp_21" }, "unique_keyname_6": { "product_owner": "co-worker-2", "update_time": "unix_timestamp_7" }, "unique_keyname_4": { "product_owner": "co-worker-3", "update_time": "unix_timestamp_75" } } } } };

// Collect all products with their unique keys
const allProducts = [];
for (const dept in obj.company) {
  const productsObj = obj.company[dept].Products;
  for (const productKey in productsObj) {
    allProducts.push({
      uniqueKey: productKey,
      ...productsObj[productKey]
    });
  }
}

Step 2: Sort by update_time (Newest First)

Next, we sort the array. Since update_time is a string-formatted Unix timestamp, we'll convert it to a number first to ensure correct numeric sorting:

// Sort from newest to oldest (higher Unix timestamp = more recent)
allProducts.sort((a, b) => {
  const timeA = Number(a.update_time.replace("unix_timestamp_", ""));
  const timeB = Number(b.update_time.replace("unix_timestamp_", ""));
  return timeB - timeA;
});

Note: I added .replace() here to strip the "unix_timestamp_" prefix—if your actual timestamps are raw numeric strings (like "1699999999"), you can skip this part and just use Number(a.update_time).

Step 3: Use the Sorted Data

Now you can work directly with the sorted array. For example, to log the results:

allProducts.forEach(product => {
  console.log(`[${product.uniqueKey}] Updated at ${product.update_time} (owned by ${product.product_owner})`);
});

Optional: Convert Back to an Object

If you need the sorted data back in object form, you can use Object.fromEntries(). Keep in mind that while modern browsers preserve object key order, this isn't guaranteed in all JavaScript environments—using the sorted array is the more reliable approach for ordered data:

const sortedProductsObj = Object.fromEntries(
  allProducts.map(product => [
    product.uniqueKey,
    { product_owner: product.product_owner, update_time: product.update_time }
  ])
);

Why You Couldn't Access the "Subarray" Values

Your original Products are objects, not arrays. Arrays use numeric indexes, while objects use string keys—so you can't iterate over them like an array without first converting them to an array of entries (which is exactly what we did in Step 1).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 07:08:08