如何在动态生成的对象字符串值上添加过滤器

huangapple go评论80阅读模式
英文:

How to add filter on a dynamically generated object strings values

问题

Sure, here's the translated code part:

我刚开始学编程我有一个动态生成的对象数据如下

data: {
    1-leadTimeNormal: 10
    3-leadTimeNormal: 22
    2-leadTimeNormal: 10
    2-leadTimeOptimal: 2
    3-leadTimeOptimal: 22
    1-leadTimeOptimal: 7
}
其中左侧的整数值132是动态生成的
现在我想要添加一个条件来检查1-leadTimeNormal是否小于1-leadTimeOptimal使用if Else),以及2-leadTimeNormal是否小于2-leadTimeOptimal等等并在控制台中使用JavaScript返回一些字符串

I've translated the code portion for you.

英文:

I am new to coding, and I have a dynamically generated Object data as this:

data: {
    1-leadTimeNormal: 10
    3-leadTimeNormal: 22
    2-leadTimeNormal: 10
    2-leadTimeOptimal: 2
    3-leadTimeOptimal: 22
    1-leadTimeOptimal: 7
}

Where, the integer value on the left hand side(1,3,2, etc) is getting generated dynamically.
Now, I want to put a condition to check whether 1-leadTimeNormal is less than 1-leadTimeOptimal or not (using if Else) and 2-leadTimeNormal is less than 2-leadTimeOptimal or not and so on and return some string in console log using javascript

答案1

得分: 1

你可以循环遍历从1到键的数量除以2的每个值。

data = {
    '1-leadTimeNormal': 10,
    '3-leadTimeNormal': 22,
    '2-leadTimeNormal': 10,
    '2-leadTimeOptimal': 2,
    '3-leadTimeOptimal': 22,
    '1-leadTimeOptimal': 7
}
let arr = Object.keys(data)

for(let i=1; i<=arr.length/2; i++) {
    if(data[`${i}-leadTimeNormal`] < data[`${i}-leadTimeOptimal`]) {
        console.log('hello world')
    } else {
        console.log('bye world')
    }
}

如果你不知道附加的数字,你可以将它们过滤掉。

data = {
    '1-leadTimeNormal': 10,
    '3-leadTimeNormal': 22,
    '2-leadTimeNormal': 10,
    '2-leadTimeOptimal': 2,
    '3-leadTimeOptimal': 22,
    '1-leadTimeOptimal': 7
}
let keys = Object.keys(data).filter(key => key.includes('leadTimeNormal'))

keys.forEach(key => {
    let num = key.replace(/^([0-9]+)-.*/, '$1')
    if (data[key] < data[`${num}-leadTimeOptimal`]) {
        console.log('hello world')
    } else {
        console.log('bye world')
    }
});
英文:

You can loop through each value from 1 to number of keys/2

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

data = {
	&#39;1-leadTimeNormal&#39;: 10,
	&#39;3-leadTimeNormal&#39;: 22,
	&#39;2-leadTimeNormal&#39;: 10,
	&#39;2-leadTimeOptimal&#39;: 2,
	&#39;3-leadTimeOptimal&#39;: 22,
	&#39;1-leadTimeOptimal&#39;: 7
}
let arr = Object.keys(data)

for(let i=1; i&lt;=arr.length/2; i++) {
	if(data[`${i}-leadTimeNormal`] &lt; data[`${i}-leadTimeOptimal`]) {
		console.log(&#39;hello world&#39;)
	} else {
		console.log(&#39;bye world&#39;)
	}
}

<!-- end snippet -->
If you dont know the appended numbers, you can filter them out
<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

data = {
	&#39;1-leadTimeNormal&#39;: 10,
	&#39;3-leadTimeNormal&#39;: 22,
	&#39;2-leadTimeNormal&#39;: 10,
	&#39;2-leadTimeOptimal&#39;: 2,
	&#39;3-leadTimeOptimal&#39;: 22,
	&#39;1-leadTimeOptimal&#39;: 7
}
let keys = Object.keys(data).filter(key =&gt; key.includes(&#39;leadTimeNormal&#39;))

keys.forEach(key =&gt; {
	let num = key.replace(/^([0-9]+)-.*/, &#39;$1&#39;)
	if (data[key] &lt; data[`${num}-leadTimeOptimal`]) {
		console.log(&#39;hello world&#39;)
	} else {
		console.log(&#39;bye world&#39;)
	}
});

<!-- end snippet -->

答案2

得分: 0

You can use 模板文字 字符串作为访问您的 data 值的键

const n = 1 // 可以是数字或字符串
if (data[`${n}-leadTimeNormal`] &gt; data[`${n}-leadTimeOptimal`]) {
    // ....
}
英文:

You can use template literal string as key to access your data value

const n = 1 // may be a number or string
if (data[`${n}-leadTimeNormal`] &gt; data[`${n}-leadTimeOptimal`]) {
    // ....
}

答案3

得分: 0

let data = {
    "1-leadTimeNormal": 10,
    "3-leadTimeNormal": 22,
    "4-leadTimeNormal": 22,
    "4-leadTimeOptimal": 42,
    "3-leadTimeOptimal": 22,
    "1-leadTimeOptimal": 7,
    "otherRandomKey": "Hello, World!"
}

// iterate over keys
for (let key in data) {
  // ensure key is in the form "x-leadTimeNormal"
  if (key.match(/\d+-.+Normal/)) {
    // get the number part
    let number = key.substring(0, key.indexOf("-"))
    // test inferiority
    if (data[number + "-leadTimeNormal"] < data[number + "-leadTimeOptimal"]) {
      console.log("log here for number " + number)
    }
  }
}
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let data = {
    &quot;1-leadTimeNormal&quot;: 10,
    &quot;3-leadTimeNormal&quot;: 22,
    &quot;4-leadTimeNormal&quot;: 22,
    &quot;4-leadTimeOptimal&quot;: 42,
    &quot;3-leadTimeOptimal&quot;: 22,
    &quot;1-leadTimeOptimal&quot;: 7,
    &quot;otherRandomKey&quot;: &quot;Hello, World!&quot;
}

// iterate over keys
for (let key in data) {
  // ensure key is in the form &quot;x-leadTimeNormal&quot;
  if (key.match(/\d+-.+Normal/)) {
    // get the number part
    let number = key.substring(0, key.indexOf(&quot;-&quot;))
    // test inferiority
    if (data[number + &quot;-leadTimeNormal&quot;] &lt; data[number + &quot;-leadTimeOptimal&quot;]) {
      console.log(&quot;log here for number &quot; + number)
    }
  }
}

<!-- end snippet -->

答案4

得分: -1

以下是您提供的代码的翻译部分:

const data = {
  '1-leadTimeNormal': 10,
  '3-leadTimeNormal': 22,
  '2-leadTimeNormal': 10,
  '2-leadTimeOptimal': 2,
  '3-leadTimeOptimal': 22,
  '1-leadTimeOptimal': 7
}
const arr = Object.keys(data);
const leadTimeNormals = arr.filter(i => i.includes("leadTimeNormal"));

leadTimeNormals.forEach(item => {
  const [number,] = item.split("-");
  if (arr[item] < arr[`${number}-leadTimeOptimal`]) {
    console.log("It is here");
  } else {
    console.log("it is not here");
  }
});

请注意,我已经忽略了注释部分,只提供了代码的翻译。

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const data = {
	&#39;1-leadTimeNormal&#39;: 10,
	&#39;3-leadTimeNormal&#39;: 22,
	&#39;2-leadTimeNormal&#39;: 10,
	&#39;2-leadTimeOptimal&#39;: 2,
	&#39;3-leadTimeOptimal&#39;: 22,
	&#39;1-leadTimeOptimal&#39;: 7
}
const arr = Object.keys(data);
const leadTimeNormals = arr.filter(i =&gt; i.includes(&quot;leadTimeNormal&quot;));

leadTimeNormals.forEach(item =&gt; {
  const [number,] = item.split(&quot;-&quot;);
  if(arr[item] &lt; arr[`${number}-leadTimeOptimal`]) {
    console.log(&quot;It is here&quot;);
  } else {
    console.log(&quot;it is not here&quot;);
  }
});

<!-- end snippet -->

Hope this helps

huangapple
  • 本文由 发表于 2020年1月3日 17:44:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576242.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定