1

I have two arrays. Those are

let x = [1,2,0,4,7,8,9,1,1,1];
let y = [10,50,80,70,906,80,70,80,15,11]

Important : x = y, That means 1 = 10, 2 = 50, 0 = 80 etc...

I want to find minimum and maximum value of x array and want to get y values related to that data, as well as 0 values (min value should greater than 0 value)

expected output is:

let res = { 
  min: {
    minVal: 1,
    minvalues: [10, 80, 15, 11]
  },
  max:  {
    minVal: 9,
    minvalues: [70]
  } 
  empty: [80]
}

Here is what I tried. this way problem is I couldn't get only value from filter method it gives object

const res = x.map((key, index) => {
  return {[key]: y[index]};
}, {});

let info = {
  min : {},
  max : {},
  empty : {}
}

let min = Math.min.apply(this, x.filter(Number));
let max = Math.max(...x);
  
info.min['min'] = min
info.min['minVal'] = res.filter((el, idx) => el[min])`
1
  • and where is labelsAndHistoData defined? Commented Feb 7, 2023 at 17:08

4 Answers 4

1

You could group and collect min and max values, then destrucure the result and build a new object.

const
    x = [ 1,  2,  0 , 4,   7,  8,  9,  1 , 1,  1],
    y = [10, 50, 80, 70, 906, 80, 70, 80, 15, 11],
    { minVal, maxVal, 0: empty, [minVal]: minvalues, [maxVal]: maxvalues } = x.reduce((r, value, i) => {
        (r[value] ??= []).push(y[i]);
        if (value && r.minVal > value) r.minVal = value;
        if (value && r.maxVal < value) r.maxVal = value;
        return r;
    }, { minVal: Number.MAX_VALUE, maxVal: -Number.MAX_VALUE })
    result = { min: { minVal, minvalues }, max: { maxVal, maxvalues }, empty };

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

Comments

0

You can store the indexes of the minimum x values, then use those indexes to get the corresponding y values with Array#map.

let x = [1,2,0,4,7,8,9,1,1,1];
let y = [10,50,80,70,906,80,70,80,15,11];
let minIdx = [0], maxIdx = [0], empty = [];
for (let i = 1; i < x.length; i++) {
  if (x[i] === 0) {
    empty.push(y[i]);
    continue;
  }
  if (x[i] === x[minIdx[0]]) minIdx.push(i);
  else if (x[i] < x[minIdx[0]]) minIdx = [i];
  if (x[i] === x[maxIdx[0]]) maxIdx.push(i);
  else if (x[i] > x[maxIdx[0]]) maxIdx = [i];
}
let res = {
  min : {
    minVal: x[minIdx[0]],
    minvalues: minIdx.map(i => y[i])
  },
  max: {
    maxVal: x[maxIdx[0]],
    maxvalues: maxIdx.map(i => y[i])
  },
  empty
};
console.log(res);

Comments

0

It looks like you're trying to do everything in one line. Let's examine finding the desired min values as separate steps.

// first remove zero values from the x array (since those appear to get their own special handing in empty
// This returns all values that are not zero in the x array
const cleanX = x.filter(val=>val !== 0);

// now find the minimum of the x array
// the ... operator takes and array and turns it into a list of parameters thus [1,2,3] becomes Math.min(1,2,3)
const xMin = Math.min(...cleanX);

// then get the values associated with the minimum X
// this filter returns only values that match xMin by using the index (position in array) to match the x and y arrays
// we have to run y.filter, to return the values from the y array
const xMinValues = y.filter((yValue, index) => x[index] === xMin);

// a portion of your result would then look like this
const res = {
  min: {
    minVal: xMin,
    minvalues: xMinValues
  },

A similar method can be used for max, and empty. But empty will only look for the fixed value of 0.

1 Comment

Thanks for your answer. I could get more idea from this
0
let x = [1, 2, 0, 4, 7, 8, 9, 1, 1, 1];
let y = [10, 50, 80, 70, 906, 80, 70, 80, 15, 11];

//group by "x" and sort
const group = x
  .reduce((a: any, b: number, index: number) => {
    const el = a.find((x) => x.value == b);
    if (!el) a.push({ value: b, data: [y[index]] });
    else el.data.push(y[index]);
    return a;
  }, [])
  .sort((a, b) => a.value - b.value);

//create an object result
const result = {
  min: {
    minVal: group[0].value == 0 ? group[1].value : group[0].value,
    minValues: group[0].value == 0 ? group[1].data : group[1].data,
  },
  max: {
    minVal: group[group.length - 1].value,
    minValues: group[group.length - 1].data,
  },
  empty: group[0].value == 0 ? group[0].data : [],
};

1 Comment

Thanks for your answer. I could get more idea from this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.