admin管理员组文章数量:1430634
I am trying to return an object based on a key, I managed to do that with the method below but still, I am wondering how to return the key and value 250: [176916, 176922, 176939],
instead of the value only [176916, 176922, 176939]
const myObj = {
250: [176916, 176922, 176939],
325: [244050],
400: [177100],
500: [166743],
700: [387789],
};
let arrAcceptedValues = [];
let notArrAcceptedValues = [];
const acceptedValues = ["250", "400"];
Object.keys(myObj).forEach((key) => {
acceptedValues.includes(key)
? arrAcceptedValues.push(...myObj[key])
: notArrAcceptedValues.push(...myObj[key]);
});
console.log({ arrAcceptedValues, notArrAcceptedValues });
//arrAcceptedValues result: [{250: [176916, 176922, 176939]}, {400: [177100]}]
I am trying to return an object based on a key, I managed to do that with the method below but still, I am wondering how to return the key and value 250: [176916, 176922, 176939],
instead of the value only [176916, 176922, 176939]
const myObj = {
250: [176916, 176922, 176939],
325: [244050],
400: [177100],
500: [166743],
700: [387789],
};
let arrAcceptedValues = [];
let notArrAcceptedValues = [];
const acceptedValues = ["250", "400"];
Object.keys(myObj).forEach((key) => {
acceptedValues.includes(key)
? arrAcceptedValues.push(...myObj[key])
: notArrAcceptedValues.push(...myObj[key]);
});
console.log({ arrAcceptedValues, notArrAcceptedValues });
//arrAcceptedValues result: [{250: [176916, 176922, 176939]}, {400: [177100]}]
Share
Improve this question
edited Dec 6, 2021 at 12:15
Abed Aarabi
asked Dec 6, 2021 at 12:09
Abed AarabiAbed Aarabi
1491 silver badge9 bronze badges
2
-
2
What do you mean by whole object? You can do
arrAcceptedValues.push({...myObj})
if you want to push the pletemyObj
– Kanishk Anand Commented Dec 6, 2021 at 12:10 - i mean, return the key and value 250: [176916, 176922, 176939], instead of the value only [176916, 176922, 176939] – Abed Aarabi Commented Dec 6, 2021 at 12:16
7 Answers
Reset to default 3Maybe this helps:
const myObj = {
250: [176916, 176922, 176939],
325: [244050],
400: [177100],
500: [166743],
700: [387789],
};
let arrAcceptedValues = [];
let notArrAcceptedValues = [];
const acceptedValues = ["250", "400"];
Object.entries(myObj).forEach(([key,value]) =>{
acceptedValues.includes(key)
? arrAcceptedValues.push({[key]: value})
: notArrAcceptedValues.push({[key]: value});
});
console.log({arrAcceptedValues , notArrAcceptedValues })
Guess you expect something like this:
const myObj = {
250: [176916, 176922, 176939],
325: [244050],
400: [177100],
500: [166743],
700: [387789],
};
let arrAcceptedValues = {};
let notArrAcceptedValues = {};
const acceptedValues = ["250", "400"];
Object.keys(myObj).forEach((key) => {
acceptedValues.includes(key)
? arrAcceptedValues[key] = [...myObj[key]]
: notArrAcceptedValues[key] = [...myObj[key]];
});
console.log({ arrAcceptedValues, notArrAcceptedValues });
//arrAcceptedValues result: [{250: [176916, 176922, 176939]}, {400: [177100]}]
Object.keys(myObj).forEach((key) => {
acceptedValues.includes(key)
? arrAcceptedValues.push({ key: myObj[key]})
: notArrAcceptedValues.push({ key: myObj[key]});
});
this will work for your case
Instead of using Object.keys, you could use Object.entries this is an easy way of getting the key & the value.
eg..
const myObj = {
250: [176916, 176922, 176939],
325: [244050],
400: [177100],
500: [166743],
700: [387789],
};
let arrAcceptedValues = {};
let notArrAcceptedValues = {};
const acceptedValues = ["250", "400"];
Object.entries(myObj).forEach(([key, value]) => {
acceptedValues.includes(key)
? arrAcceptedValues[key] = value
: notArrAcceptedValues[key] = value;
});
console.log({ arrAcceptedValues, notArrAcceptedValues });
const myObj = {
250: [176916, 176922, 176939],
325: [244050],
400: [177100],
500: [166743],
700: [387789],
};
let arrAcceptedValues = {};
let notArrAcceptedValues = {};
const acceptedValues = ["250", "400"];
Object.keys(myObj).forEach((key) => {
if (!(arrAcceptedValues[key] instanceof Array) && acceptedValues.includes(key)) {
arrAcceptedValues[key] = [];
}
if (!(notArrAcceptedValues[key] instanceof Array) && !acceptedValues.includes(key)) {
notArrAcceptedValues[key] = [];
}
acceptedValues.includes(key)
? arrAcceptedValues[key].push(...myObj[key])
: notArrAcceptedValues[key].push(...myObj[key]);
});
console.log({ arrAcceptedValues, notArrAcceptedValues });`enter code here`
With for in loop
const myObj = {
250: [176916, 176922, 176939],
325: [244050],
400: [177100],
500: [166743],
700: [387789],
};
let arrAcceptedValues = {};
let notArrAcceptedValues = {};
const acceptedValues = ["250", "400"];
for(let key in myObj){
acceptedValues.includes(key)
? arrAcceptedValues[key] = myObj[key]
: notArrAcceptedValues[key] = myObj[key];
}
console.log({ arrAcceptedValues, notArrAcceptedValues });
const myObj = {
250: [176916, 176922, 176939],
325: [244050],
400: [177100],
500: [166743],
700: [387789],
};
let arrAcceptedValues = [];
let notArrAcceptedValues = [];
const acceptedValues = ["250", "400"];
Object.keys(myObj).forEach((key) => {
acceptedValues.includes(key)
? arrAcceptedValues.push({ [key]: myObj[key]})
: notArrAcceptedValues.push({ [key] : myObj[key]});
});
console.log({ arrAcceptedValues, notArrAcceptedValues });
// {"arrAcceptedValues":[{"250":[176916,176922,176939]},{"400":[177100]}],"notArrAcceptedValues":[{"325":[244050]},{"500":[166743]},{"700":[387789]}]}
本文标签: javascripthow to return an object from a keyStack Overflow
版权声明:本文标题:javascript - how to return an object from a key - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745460893a2659321.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论