javascript - How to remove $$hashKey from objects? - Stack Overflow

Ramda REPL exampleHi, so I have an Array of objects. These objects somehow gain a $$hashKey. I need to

Ramda REPL example

Hi, so I have an Array of objects. These objects somehow gain a $$hashKey. I need to remove the $$hashKey so my drop duplicates or remove uniques logic.

let tickers = [
  {
    $$hasKey: "object:280",
    ticker: 'goog'
  },
  { 
    $$hasKey: "object:308",
    ticker: 'goog'
  },
  {
    $$hasKey: "object:327",
    ticker: 'goog'
  }
]

R.uniq(tickers); //-> Returns all instead of 1

R.dropRepeats(tickers); //-> Returns all instead of 1

I tried this, but then my tickers array ended up with 3 undefined values.

const removeRepeats = tickers => {
    console.log('removeRepeats', tickers);
    tickers = _.map(tickers, ticker => {
        delete ticker['$$hasKey']; 
    });

    return R.dropRepeats(tickers);
};

Ramda REPL example

Hi, so I have an Array of objects. These objects somehow gain a $$hashKey. I need to remove the $$hashKey so my drop duplicates or remove uniques logic.

let tickers = [
  {
    $$hasKey: "object:280",
    ticker: 'goog'
  },
  { 
    $$hasKey: "object:308",
    ticker: 'goog'
  },
  {
    $$hasKey: "object:327",
    ticker: 'goog'
  }
]

R.uniq(tickers); //-> Returns all instead of 1

R.dropRepeats(tickers); //-> Returns all instead of 1

I tried this, but then my tickers array ended up with 3 undefined values.

const removeRepeats = tickers => {
    console.log('removeRepeats', tickers);
    tickers = _.map(tickers, ticker => {
        delete ticker['$$hasKey']; 
    });

    return R.dropRepeats(tickers);
};
Share Improve this question edited Sep 22, 2016 at 23:37 Leon Gaban asked Sep 22, 2016 at 23:28 Leon GabanLeon Gaban 39.1k122 gold badges349 silver badges550 bronze badges 4
  • Does "ramda" have a map()? – JonSG Commented Sep 22, 2016 at 23:39
  • Yes ramdajs./docs/#map – Leon Gaban Commented Sep 22, 2016 at 23:39
  • 1 What happens if you update removeRepeats with : tickers = _.map(tickers, ticker => { return {ticker: ticker["ticker"]}; }); – JonSG Commented Sep 22, 2016 at 23:55
  • Haha yes that did work! Lol sorry I see what happened here... I got so tunnel visioned on removing / destroying those $$hashKey keys that I forgot I could just do this. Want to post your answer? – Leon Gaban Commented Sep 22, 2016 at 23:59
Add a ment  | 

3 Answers 3

Reset to default 3

I'm assuming that you really want uniq rather than dropRepeats. uniq gives you the collection of different element in a list. dropRepeats simply removes sequential copies of the same value. ([1, 1, 2, 3, 3, 3, 2, 2, 1] => [1, 2, 3, 2, 1]).

To do this, I would think of it as two steps, one to create versions without the offending key (Ramda's dissoc should help with this) and one to reduce the list to the uniq elements. I might write it like this:

let myFunc = R.pipe(R.map(R.dissoc('$$hasKey')), R.uniq);
myFunc(tickers);

If you didn't want to simply remove a single property but instead keep a fixed list of them you might do:

R.pipe(R.map(R.pick(['ticker', 'prop2', 'prop3'])), R.uniq)

I would try:

const removeRepeats = tickers => {
    console.log('removeRepeats', tickers);
    tickers = _.map(tickers, ticker => { return {ticker: ticker["ticker"]}; });
    return R.dropRepeats(tickers);
};

I don't know why you need both lodash and ramda together. Any of lodash or ramda by itself is enough with one liner

lodash:

_.uniqWith(_.map(tickers, (ticker) => _.pick(ticker, ['ticker'])), _.isEqual)

ramda:

R.uniq(R.map(R.pick(['ticker']))(tickers))

or if you want to pare without all properties starting with $$ (angular internal properties)

lodash:

_.uniqWith(_.map(tickers, (ticker) => _.pickBy(ticker, (val, key) => !_.startsWith(key, '$$'))), _.isEqual)

ramda:

R.uniq(R.map(R.pickBy((v, k) => !k.startsWith('$$')))(tickers))

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744756333a4591904.html

相关推荐

  • javascript - How to remove $$hashKey from objects? - Stack Overflow

    Ramda REPL exampleHi, so I have an Array of objects. These objects somehow gain a $$hashKey. I need to

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信