英文:
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
}
其中,左侧的整数值(1、3、2等)是动态生成的。
现在,我想要添加一个条件来检查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 = {
'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')
}
}
<!-- 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 = {
'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')
}
});
<!-- end snippet -->
答案2
得分: 0
You can use 模板文字 字符串作为访问您的 data
值的键
const n = 1 // 可以是数字或字符串
if (data[`${n}-leadTimeNormal`] > 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`] > 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 = {
"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)
}
}
}
<!-- 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 = {
'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");
}
});
<!-- end snippet -->
Hope this helps
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论