小编Zig*_*ers的帖子

需要帮助理解lodash的_.includes方法

我想_.includes在我的代码中使用lodash的方法,但是任何时候我都有一个对象数组,我无法让它工作,而是最终依赖于该_.find方法.

从我的测试中我只能_.includes使用简单的数组.但也许这就是它的工作方式?

我对Lodash和编程很新,所以我想如果我错过了一些关于如何使用这种方法的话,我会问.

我使用以下代码创建了一个jsbin:http://jsbin.com/regojupiro/2/

var myArray = [];

function createArray(attName, attId, otherId) {
    var theObject = {};

    theObject.attributeName = attName;
    theObject.attributeId = attId;
    theObject.otherId = [otherId];

      return theObject;
}

myArray.push(createArray('first', 1001, 301));
myArray.push(createArray('second', 1002, 302));
myArray.push(createArray('third', 1003, 303));
myArray.push(createArray('fourth', 1004, 304));

var isPresent1 = _.includes(myArray, {'attribtueId' : 1001});
var isPresent2 = _.includes(myArray, 1001);
var found = _.find(myArray, {'attributeId' : 1001});

console.log(isPresent1);
console.log(isPresent2);
console.log(found);
console.log(myArray);
Run Code Online (Sandbox Code Playgroud)

两个"isPresent"变量都返回false,但该_.find方法返回正确的对象.

_.includes当我只需要进行简单的真/假检查以查看我的对象数组中是否存在值时,我希望能够更好地理解如何使用该方法.

或者,如果这是工作的错误工具,该_.find方法是适合这项工作的工具,还是其他一些我不熟悉的lodash方法? …

javascript lodash

5
推荐指数
2
解决办法
7376
查看次数

如何在react-router中正确处理子路由(路由工作,但每次点击都会增加URL)

我正在学习如何使用react-router.我的路由工作正常,这意味着当我单击相应的链接时,我会获得所需的视图.我有一个嵌套/子路由的视图.单击这些子链接会将我带到相应的视图,但它不会按照我的意愿更新浏览器的URL.

我从视图的IndexRoute开始.浏览器的URL看起来像这样,这就是我想要的:

http://localhost:3000/administration

当我点击不同的子路由时,它会不断将子路由的路径附加到URL上,这样在几次单击后,浏览器的URL最终会看起来像这样:

http://localhost:3000/administration/administration/administration/administration/administration/administration/administration/administration/company-goals

当我想要的是浏览器的URL是这样的:

http://localhost:3000/administration/company-goals

我有一个如下设置的路由结构:

<Route path="/" component={HomePage}> <IndexRoute component={HomeView} /> <Route path="budgets" component={BudgetView} /> <Route path="administration" component={adminViews.Administration}> <IndexRoute component={adminViews.Home} /> <Route path="department-budgets" component={adminViews.DepartmentBudgets} /> <Route path="price-list" component={adminViews.PriceList} /> <Route path="company-goals" component={adminViews.CompanyGoals} /> </Route> </Route>

"管理"视图具有选项卡式导航,其中包含每个"管理"视图的子路径的选项卡.

当用户单击具有如下函数的选项卡时,我正在更改路径:

selectedTab( tab ) { const menu = menuItems.find((item) => { return item.payload === tab.value; }); history.pushState(null, menu.uriPath); }

并且每个路由的uriPath定义如下:

menuItems = [ { payload : '1', text : 'Home', uriPath : 'administration' }, { payload : '2', text : 'Department …

reactjs react-router

4
推荐指数
1
解决办法
591
查看次数

学习Objective-C:需要有关填充NSMutableDictionary的建议

我正在使用一些资源自学Objective-C,其中一个是通过iTunes U(2010年过去的课程)提供的Stanford iPhone Dev课程.

其中一项家庭工作任务要求我使用预定义的键和值列表(URL)填充可变字典.我能够将代码放在一起,但是当我看到它时,我一直认为可能有更好的方法让我接近我想要做的事情:

  1. 使用预定义的键和值填充NSMutableDictionary
  2. 枚举字典中的键并检查每个键,看它是否以"斯坦福"开头
  3. 如果符合条件,请记录密钥和值

我真的很感激有关如何改进我所放在一起的任何反馈.我是初学者的定义,但我真的很享受学习Objective-C的挑战.

void bookmarkDictionary () {
    NSMutableDictionary* bookmarks = [NSMutableDictionary dictionary];

    NSString* one = @"Stanford University",
    *two = @"Apple",
    *three = @"CS193P",
    *four = @"Stanford on iTunes U",
    *five = @"Stanford Mall";

    NSString* urlOne = @"http://www.stanford.edu",
    *urlTwo = @"http://www.apple.com",
    *urlThree = @"http://cs193p.stanford.edu",
    *urlFour = @"http://itunes.stanford.edu",
    *urlFive = @"http://stanfordshop.com";

    NSURL* oneURL = [NSURL URLWithString:urlOne];
    NSURL* twoURL = [NSURL URLWithString:urlTwo];
    NSURL* threeURL = [NSURL URLWithString:urlThree];
    NSURL* fourURL = [NSURL URLWithString:urlFour];
    NSURL* fiveURL = [NSURL URLWithString:urlFive]; …
Run Code Online (Sandbox Code Playgroud)

objective-c

2
推荐指数
1
解决办法
1951
查看次数

单个页面上有多个列表组,但每个列表组允许唯一的选择

我想在我的页面上有三个单列列表组,并允许用户从每个组中选择一个项目。但是,列表组都是链接的,我无法弄清楚如何将它们保留为单独的列表组。

我在这里设置了一个快速Plunker 链接,以便您可以看到我正在尝试做什么。

我也不确定为什么列表的每一列都会被前一列偏移。

这是我在尝试弄清楚如何执行此操作时正在使用的 HTML:

<div class="container">
    <h3>
        Select one from each column.
    </h3>

    <div>
      <h3 class="list-group-item-heading">Select Opp</h3>
    </div>
    <div id="oppname" class="col-md-4 list-group">
      <div class="no-margin">
        <a href="#" class="list-group-item">
                Aelia
            </a>
      </div>
      <div class="no-margin">
        <a href="#" class="list-group-item">
                Del Monte
            </a>
      </div>
    </div>

    <div>
      <h3 class="list-group-item-heading">Select Id</h3>
    </div>
    <div id="oppid" class="col-md-4 list-group">
      <div class="no-margin">
        <a href="#" class="list-group-item">
                Id: 1724
            </a>
        <div class="no-margin">
          <a href="#" class="list-group-item">
                Id: 1675
            </a>
        </div>
      </div>
    </div>

    <div>
      <h3 class="list-group-item-heading">Select Size</h3>
    </div>
    <div id="oppsize" class="col-md-4 …
Run Code Online (Sandbox Code Playgroud)

html twitter-bootstrap-3

1
推荐指数
1
解决办法
1549
查看次数