繁体   English   中英

在javascript中的匹配数组中查找最大值和最小值

[英]Find max and min within matching array in javascript

我有一个像下面这样的对象...

var SpecialObj=
{   
    "dataarray-345":
    [
        {lqd: 1000, date: "2017-08-18 09:30:00"},
        {lqd: 5000, date: "2017-08-18 10:00:00"},
        {lqd: 5500, date: "2017-08-18 10:30:00"},
        {lqd: 4500, date: "2017-08-18 11:00:00"},
        {lqd: 1500, date: "2017-08-18 11:30:00"}
    ],
    "dataarray-123":
    [

        {lqd: 5000, date: "2017-08-18 10:00:00"},
        {lqd: 5500, date: "2017-08-18 10:30:00"},
        {lqd: 4500, date: "2017-08-18 11:00:00"},
        {lqd: 1500, date: "2017-08-18 11:30:00"},
        {lqd: 2500, date: "2017-08-18 12:00:00"},
        {lqd: 3500, date: "2017-08-18 12:30:00"}
    ],
    "dataarray-127":
    [
        {lqd: 8500, date: "2017-08-18 13:00:00"},
        {lqd: 9500, date: "2017-08-18 13:30:00"},
        {lqd: 6500, date: "2017-08-18 14:00:00"}
    ]
}

现在我需要检查单个数组是否第一个最后一个索引与其他其他数组相同,如果是,则需要确定最大值和最小值。 如果没有,则需要分别确定该特定数组的最大值和最小值。 对于上面的对象,下面应该是我的输出...

{   
    "dataarray-345":
    [
        {lqd: 1000, date: "2017-08-18 09:30:00"},
        {lqd: 3500, date: "2017-08-18 12:30:00"}
    ],
    "dataarray-123":
    [
        {lqd: 1000, date: "2017-08-18 09:30:00"},
        {lqd: 3500, date: "2017-08-18 12:30:00"}
    ],
    "dataarray-127":
    [
        {lqd: 8500, date: "2017-08-18 13:00:00"},
        {lqd: 6500, date: "2017-08-18 14:00:00"}
    ]
}

在javascript中可能吗? javascript中的方法是什么? 你能帮我吗?

边缘有点粗糙,没有时间添加评论和优化,但它有效。

该代码段的日志有点奇怪,但是当您打开开发工具时,您将看到预期的输出。

 const SpecialObj = { "dataarray-345": [ {lqd: 1000, date: "2017-08-18 09:30:00"}, {lqd: 5000, date: "2017-08-18 10:00:00"}, {lqd: 5500, date: "2017-08-18 10:30:00"}, {lqd: 4500, date: "2017-08-18 11:00:00"}, {lqd: 1500, date: "2017-08-18 11:30:00"} ], "dataarray-123": [ {lqd: 5000, date: "2017-08-18 10:00:00"}, {lqd: 5500, date: "2017-08-18 10:30:00"}, {lqd: 4500, date: "2017-08-18 11:00:00"}, {lqd: 1500, date: "2017-08-18 11:30:00"}, {lqd: 2500, date: "2017-08-18 12:00:00"}, {lqd: 3500, date: "2017-08-18 12:30:00"} ], "dataarray-127": [ {lqd: 8500, date: "2017-08-18 13:00:00"}, {lqd: 9500, date: "2017-08-18 13:30:00"}, {lqd: 6500, date: "2017-08-18 14:00:00"} ] }; /** * Creates a reverse lookup object. For each unique lqd value it will list the * keys that contain the lqd value. * * @param {Object} input The input object to create a reverse lookup for. * * @returns {Object} An object where each unique lqd value is a property with * it's value an array of object keys. */ function getReverseLookupObject(input) { // Create the result object. const result = Object.create(null); // Iterate over the object properties. for (let key in input) { // When the current property is from the prototype chain we will skip it. if (!input.hasOwnProperty(key)) { continue; } // Iterate over all the items in the array that is stored under the property. input[key].forEach(item => { // Check if the current lqd is not yet in the result. if (result[item.lqd] === undefined) { // Create a property for the lqd value and assign it an array with the current key. result[item.lqd] = [key]; } else { // Add the current key to the array. result[item.lqd].push(key); } }); } // Return the reverse lookup object. return result; } /** * Returns an array with two items. The item at index 0 will be the item with the * earliest date. Index 1 will contain the item with the latest date. * * @param {Array} array The array with source items. * * @returns {Array} An array with the items that have the earliest and latest date. */ function getMinMax(array) { // Sort the source array by date property, ascending. array.sort((a, b) => new Date(a.date).getTime() - new Date(b.date).getTime()); // Return an array with the first and last item in the sorted array. return [ array[0], array[array.length - 1] ] } /** * Returns a set of properties in the original input object that contain the * lqd of either the first or last item in the array. */ function getParentKeys(reverseLookupObject, keyValues) { const // Get last index if the keyValue array. lastIndex = keyValues.length - 1, // Get the array with keys that contain the lqd for the 1st item. firstElementParents = reverseLookupObject[keyValues[0].lqd], // Get the array with keys that contain the lqd for the last item. lastElementParents = reverseLookupObject[keyValues[lastIndex].lqd]; // Create a set of the keys. This has the benefit that duplicate keys will // be removed. When the first and last element both have the same parent the // resulting set will only contain that parent once. return new Set([...firstElementParents, ...lastElementParents]); } function transformData(input) { const // Create the reverse lookup object. reverseLookup = getReverseLookupObject(input), // Initialize the result. result = Object.create(null); // Iterate over the properties of the input object. for (let key in input) { // When the property if from the prototype chain we can skip it. if (!input.hasOwnProperty(key)) { continue; } const // Get the input object properties that contain the lqd of the first and last items. parentKeys = getParentKeys(reverseLookup, input[key]); let combinedItems = []; // Iterate over the parents and combine all their values into a single array. parentKeys.forEach(parentKey => combinedItems = [...combinedItems, ...input[parentKey]]); // Add the current key in the result with the items that have the earliest and // latest date. result[key] = getMinMax(combinedItems); } return result; } console.log(transformData(SpecialObj));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM