通过比较两个数组生成一个数组并动态添加一个键

时间:2016-06-16 09:10:17

标签: javascript arrays angularjs

我的应用从API接收数据并将其保存在$scope.newz中。

bookmarkData从LocalStorage加载。

我正在比较两个数组的contentId值, $ scope.newz $ scope.bookmarkData

我无法确定如何仅为匹配的数组项保存bookmarkstate : true

最后,匹配的记录将被添加到$scope.AllnewsList

这是我的代码

if(data.Status.ResponseCode == 200)
  { 
    $("#fetchnews").hide();

    // $("#nodata").show();

    $("#sError").hide();

    //$scope.AllnewsList =  data.contents;

    $scope.newz = data.contents; 
    $scope.bookmarkData = JSON.parse(window.localStorage.getItem('bookmark'));

    for (var i=0; i < $scope.newz.length; i++)
    {
      for (var j=0; j < $scope.bookmarkData.data.length; j++) 
      {
        if ($scope.newz[i].ContentId == $scope.bookmarkData.data[j].ContentId)
          {
            // console.log($scope.newz[i].ContentId);
            $scope.bookmarkstate == true; 
          }
          else
          {
            $scope.bookmarkstate == false;
          }
      }
    }
    $scope.AllnewsList = $scope.newz;
  }

3 个答案:

答案 0 :(得分:0)

如果我理解正确,您正在寻找注册每个newz ,无论您是否至少有一个书签。

如果这是您想要做的,我建议您将$scope.bookmarkstate设置为与newz数组相同大小的数组。

这样,如果"newz"数组中的相应单元格包含书签,您可以在其每个单元格中注册。

if(data.Status.ResponseCode == 200)
    { 
       $scope.bookmarkstate = [];
       $("#fetchnews").hide();
       $("#sError").hide();
      $scope.newz = data.contents; 
      $scope.bookmarkData = JSON.parse(window.localStorage.getItem('bookmark'));
       for (var i=0; i < $scope.newz.length; i++)
        {
          for (var j=0; j < $scope.bookmarkData.data.length; j++) 
          {
              if ($scope.newz[i].ContentId == $scope.bookmarkData.data[j].ContentId)
               {
                 $scope.bookmarkstate[i] == true; 
              }
              else
              {

                 $scope.bookmarkstate[i] == false;
              }
          }
      }
       $scope.AllnewsList = $scope.newz;
   }

让我知道这对您有何影响。 欢呼声。

答案 1 :(得分:0)

假设您在评论中提到的密钥并假设动态数据,我按以下方式定义了数组。

如果我误解了你的问题,请纠正我

&#13;
&#13;
var newz = [{contentId : 1, source : 'demoSrc1', title:'title1' },
{contentId : 2, source : 'demoSrc2', title:'title2' },
{contentId : 3, source : 'demoSrc3', title:'title3' },
{contentId : 4, source : 'demoSrc4', title:'title4' },
{contentId : 5, source : 'demoSrc5', title:'title5' },
{contentId : 6, source : 'demoSrc6', title:'title6' },
{contentId : 7, source : 'demoSrc7', title:'title7' },
{contentId : 8, source : 'demoSrc8', title:'title8' }];
//I defined your newz statically for simplicity.

var bookmarkData = [{contentId : 1, source : 'demoSrc1', title:'title1' },
{contentId : 4, source : 'demoSrc4', title:'title4' },
{contentId : 6, source : 'demoSrc6', title:'title6' },
{contentId : 8, source : 'demoSrc8', title:'title8' }];
//SIMILARLY id defined your bookmarkData statically.


var AllnewsList = [];


    for (var i=0; i <  newz.length; i++)
    {
      for (var j=0; j < bookmarkData.length; j++) 
      {
            if (newz[i].contentId == bookmarkData[j].contentId)
          {

            newz[i].bookmarkstate = true; 
            AllnewsList.push(newz[i]);
//            console.log(newz[i]);
          }
          else
          {
            newz[i].bookmarkstate = false;
          }
  //        console.log(newz[i].contentId);

      }
    }
console.log(AllnewsList);
&#13;
&#13;
&#13;

答案 2 :(得分:0)

Tip : You are not using assignment operator in your code to store status and i will recommend you to use
underscore for this tasks if you want to minimize lines of code.

If i understand you correctly the bookmarkstate array  will have true value for each news if news ContentId is equal to each bookmarkData ContentId else it will store false for each news.
var bookmarkstate = [];
var newz = [
    {
        id: 1,
        name: "first",
        ContentId: 3
    },

    {
        id: 2,
        name: "second",
        ContentId: 33
    },
    {
        id: 3,
        name: "third",
        ContentId: 3
    }

];
var bookmarkData = [
    {
        id: 1,
        name: "first",
        ContentId: 3
    },

    {
        id: 2,
        name: "second",
        ContentId: 33
    },
    {
        id: 3,
        name: "third",
        ContentId: 3
    }

];

for (var i = 0; i < newz.length; i++) {
    for (var j = 0; j < bookmarkData.length; j++) {
        if (newz[i].ContentId == bookmarkData[j].ContentId) {
            bookmarkstate[i] = true; // you were not using assignment operator
        } else {
            bookmarkstate[i] = false;// ou were not using assignment operator
        }

    }
}
console.log(bookmarkstate);