Javascript按键拆分对象 [英] Javascript split object by key

查看:37
本文介绍了Javascript按键拆分对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有波纹管对象:

   var obj = {
         name:'Jone',
         age:'23',
         height:200,
         weight:400,
   }

我想要这个:

   var obj1 = {
         name:'Jone',
         age:'23'
   }

   var obj2 = {
         height:200,
         weight:400
   }

我真正要找的是 obj.splice('age') 意思是把这个对象分成两个对象,从 age 开始作为键.

Really what I'm looking for is obj.splice('age') meaning that split this object into two object , starting from age as the key.

我知道我可以循环并以艰难的方式做到这一点,但是有没有等效的简单好方法?

I know I can loop and do it the hard way, but is there any equivalent easy nice way ?

另外,如果有任何 Angular2ish 方式(可能是隐藏的实用程序?),会更好.

Also, if there is any Angular2ish way(a hidden utility maybe ? ), would be even better.

我不完全知道对象里面是什么,我只知道我的钥匙在里面.

EDIT : I don't exactly know what's inside the object , I just know my key is in there .

我真正在做的是我想围绕一个对象循环,但我有一个起点,我不想遍历所有对象,我希望能够剪切该对象并从哪里开始循环我需要.

What I really am doing is I want to loop around an object , but I have an start point and i don't want to loop through all , I want to be able to cut that object and just start the loop from where I need.

    Object.keys(obj).map( function( value , index ) {
       // I'm just looping here to do some stuff , which is not related to the question here.


    } );

显然我正在遍历这个对象的所有键,但我想从 age 开始.

Obviously I'm looping over all the keys of this object , but I want to start from age .

我希望这很清楚.

编辑;我知道这有点过时了,但是在 Angular2 中,我们有一个 formModel 并且里面可以有多个 controlGroup ,我希望基本上能够找到第一个无效的 controlGroup ,就是这样.

推荐答案

PLUNKER 演示

根据我对您的问题的了解和理解,

From what I know and understood from your problem,

ControlGroup 是一个 Object 并且其他人也注意到属性没有特定的顺序,你不能依赖即使你知道它是按字母顺序.

ControlGroup is an Object and as others have also noted that properties don't have a specific order, and you can't rely on it even if you know that it's in alphabetical order.

我假设您要么使用 formBuilder 编写 controlGroups,要么使用循环生成.

I assume that you either write the controlGroups using formBuilder or generate using a loop.

  • 如果你已经有一个 controlGroups 的数组,只需将它们保存在一个像
  • 这样的数组中
  • If you already have an array of what controlGroups are there going to be, just save them in an array like

cgs = ['controlGroup1', 'controlGroup2', 'controlGroup3'] 

按照您将它们放入 template 的确切顺序.现在您知道自己在寻找什么了.

in the exact order that you will put them in the template. Now you know what you are looking for.

  • 如果不这样做,
    • 你可以创建一个数组
    • 如果你不能,就像遍历 DOM
    • If you don't,
      • You can create an array
      • If you can't, just traverse the the DOM like

      let domList = document.querySelectorAll('*[ngControlGroup]');
      let cgEls = Array.apply(null, domList);
      var cgs = [];  // will store the ordered list of controlGroups
      
      cgEls.forEach((e) => {
       cgs.push(e.attributes.ngControlGroup.value);
      })
      

      现在您知道订单了,请一一检查其有效性.

      NOW you know the order, check for thier validity one by one.

      希望有帮助.

      这篇关于Javascript按键拆分对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆