最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to return an object from a key - Stack Overflow

programmeradmin1浏览0评论

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 plete myObj – 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
Add a ment  | 

7 Answers 7

Reset to default 3

Maybe 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]}]}
发布评论

评论列表(0)

  1. 暂无评论