选择要成为对象属性的数组

时间:2017-05-31 13:08:28

标签: javascript arrays object

我获得了一个数组和一个对象,我正在创建一个函数,该函数返回一个新对象,该对象的属性是给定对象中的属性,其键存在于给定数组中。如果键存在于给定数组中,但不在给定对象中,则应忽略它们。它不会修改传入的对象。

到目前为止,我得到了这个:

store.dispatch(makeASandwichWithSecretSauce("My wife")).then(() => {

  const ctx = ReactDomServer.renderToString(
    <Provider store={store}>
      <StaticRouter context={context} location={req.url}>
        <App />
      </StaticRouter>
    </Provider>
  );

  const reactHelmet = ReactHelmet.renderStatic();

  const page = renderOutput(ctx, finalState, reactHelmet);

  res.status(status).send(page);
});

知道我做错了什么吗?

2 个答案:

答案 0 :(得分:2)

你差不多了,

  • 如果您添加一个检查键是否在对象(in operator)中,然后
  • 您需要对返回对象的新属性进行赋值,至少需要
  • 你需要归还那个物体。

&#13;
&#13;
        TabControl tc = TC_Fields;
        TabPage tpOld = tc.SelectedTab;

        TabPage tpNew = new TabPage();

        fields += 1;
        tpNew.Name = "Field_" + fields;
        tpNew.Text = "Field-" + fields;

        foreach (Control c in tpOld.Controls)
        {
            Control cNew = (Control) Activator.CreateInstance(c.GetType());
            PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(c);
            foreach (PropertyDescriptor entry in pdc)
            {
                object val = entry.GetValue(c);
                if (entry.Name == "Name")
                {
                    val = (String) val + fields;
                }
                else if (entry.Name == "Location" || entry.Name == "Text" || entry.Name == "Bounds" || entry.Name == "Enabled" 
                    || entry.Name == "Visible" || entry.Name == "Checked" || entry.Name == "CheckState")
                {
                    //Nothing to do, but do continue!
                }
                else if (entry.Name == "Controls")
                {
                    Control.ControlCollection controllsInside = (Control.ControlCollection) val;
                    foreach (Control controllInside in controllsInside)
                    {
                        Control cNewInside = (Control) Activator.CreateInstance(controllInside.GetType());
                        PropertyDescriptorCollection pdcInside = TypeDescriptor.GetProperties(controllInside);
                        foreach (PropertyDescriptor entryInside in pdcInside)
                        {
                            object valInside = entryInside.GetValue(controllInside);
                            if (entryInside.Name == "Name")
                            {
                                valInside = (String) valInside + fields;
                            }
                            else if (entryInside.Name == "Location" || entryInside.Name == "Text" || entryInside.Name == "Bounds" || entryInside.Name == "Enabled" 
                                || entryInside.Name == "Visible" || entryInside.Name == "Checked" || entryInside.Name == "CheckState")
                            {
                                //Nothing to do, but do continue!
                            }
                            else
                            {
                                continue;
                            }
                            entryInside.SetValue(cNewInside, valInside);
                        }
                        cNew.Controls.Add(cNewInside);
                    }
                }
                else
                {
                    continue;
                }
                entry.SetValue(cNew, val);
            }
            tpNew.Controls.Add(cNew);
        }

        tc.TabPages.Add(tpNew);
        TC_Fields.SelectedIndex = fields - 1;
&#13;
&#13;
&#13;

答案 1 :(得分:0)

这里是ES6的一个班轮;)

&#13;
&#13;
var arr = ['a', 'c', 'e'];
var obj = {
  a: 1,
  b: 2,
  c: 3,
  d: 4
};

var r = Object.keys(obj).reduce((o, v) => {return (arr.some(a => {return a===v;}) && (o[v] = obj[v]), o)}, {});

console.log(r);
&#13;
&#13;
&#13;